MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
jookr
V2EX  ›  MySQL

Mysql 里 insert into select from 这种用法的特殊需求该怎么写?

  •  
  •   jookr · Apr 21, 2015 · 5218 views
    This topic created in 4066 days ago, the information mentioned may be changed or developed.

    table
    id name telphone age
    1 tom 138 12
    2 bill 139 13

    不能一步完成的方法是先查出来,再按需求插入
    $data = $db->get_one("select * from table where name='tom' limit 1");
    $db->query("insert into table(name,telphone,age) values('$data[name]','1388888‘,'$data[age]')");

    Insert into table(name,telphone,age) select name,telphone,age from table where name='tom'
    这句能正常使用完全复制值插入的

    但是我想几个字段自定义值(如上例,telphone不用查询出来的值而是另外定义) 请问这语句该怎么改

    先谢谢了!

    8 replies    2015-04-21 15:42:15 +08:00
    caoyue
        1
    caoyue  
       Apr 21, 2015   ❤️ 1
    Insert into table(name,telphone,age) select name,'1388888‘,age from table where name='tom'
    jookr
        2
    jookr  
    OP
       Apr 21, 2015
    @caoyue 行不通 报错 还有其他方法吗
    不过还是非常感谢
    caixiexin
        3
    caixiexin  
       Apr 21, 2015   ❤️ 1
    1l的方法为啥不行呢,是不是lz你自定义的值跟表字段不符?我记得以前我也是这么写的
    liuhaotian
        4
    liuhaotian  
       Apr 21, 2015 via iPhone   ❤️ 1
    @jookr 他的一个引号不是英文的 你改了吗 应该没问题的。
    提供下表结构
    jookr
        5
    jookr  
    OP
       Apr 21, 2015
    @caoyue
    @caixiexin
    @liuhaotian
    确实1L的符号问题
    非常感谢各位
    caoyue
        6
    caoyue  
       Apr 21, 2015   ❤️ 1
    @jookr
    我从你上面复制的=-= 没注意
    bombless
        7
    bombless  
       Apr 21, 2015   ❤️ 1
    我晕,mysql里面反引号是用来围住名字(好像叫标识符)的,不能用来围住关键字。

    话说楼主是呜呜吗?
    jookr
        8
    jookr  
    OP
       Apr 21, 2015
    @bombless 呜呜?不认识
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   990 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 22:27 · PVG 06:27 · LAX 15:27 · JFK 18:27
    ♥ Do have faith in what you're doing.