从2表到表1的列插入

时间:2016-08-22 07:14:42

标签: mysql syntax-error

我有两张桌子:

checkinout

Userid | Checktime | Checktype | VERIFYCODE | SENSORID | Memoinfo | WorkCode | sn |

userinfo

Userid | Name | Gender

我想从userinfo表中获取列名,并将其插入checkinout表,对应于他们的Userid,checkinout.userid=userinfo.userid

以下是我的查询,但我在userinfo周围遇到语法错误,你能说出我错过了什么吗?

  

语法错误:意外' userinfo' (标识符)

SELECT checkinout.USERID, checkinout.CHECKTIME, checkinout.CHECKTYPE,checkinout.VERIFYCODE, checkinout.SENSORID, checkinout.Memoinfo, checkinout.WorkCode, checkinout.sn, userinfo.name 
from bio_raw.checkinout, bio_raw.userinfo
join bio_raw.userinfo
on checkinout.userid = userinfo.userid 

5 个答案:

答案 0 :(得分:3)

SELECT 
  cio.`userid`,
  cio.`checktime`,
  cio.`checktype`,
  cio.`memoinfo`,
  cio.`sensorid`,
  cio.`sn`,
  cio.`verifycode`,
  cio.`workcode`,
  ui.`name`,
  ui.`gender` 
FROM
  checkinout cio 
  INNER JOIN userinfo ui 
    ON ui.`userid` = cio.`userid` 

答案 1 :(得分:2)

你不能混合explizit和implizit join:

SELECT CI.USERID
    ,CI.CHECKTIME
    ,CI.CHECKTYPE
    ,CI.VERIFYCODE
    ,CI.SENSORID
    ,CI.Memoinfo
    ,CI.WorkCode
    ,CI.sn
    ,UI.NAME
FROM bio_raw.checkinout AS CI
JOIN bio_raw.userinfo AS UI ON CI.userid = UI.userid

答案 2 :(得分:1)

在第crumb行中,删除from bio_raw.checkinout, bio_raw.userinfo

  • 使用, bio_raw.userinfo时,JOIN无需提及多个表名。
  • 为了更好的可读性,我添加了表别名FROMCI

工作代码将是:

UI

答案 3 :(得分:1)

当你想加入2个表时,不需要将它们全部放在FROM param中。

SELECT checkinout.USERID, checkinout.CHECKTIME, checkinout.CHECKTYPE,checkinout.VERIFYCODE, checkinout.SENSORID, checkinout.Memoinfo, checkinout.WorkCode, checkinout.sn, userinfo.name 
   from bio_raw.checkinout
   join bio_raw.userinfo
   on checkinout.userid = userinfo.userid 

答案 4 :(得分:1)

userinfo子句中不需要提及FROM表。使用以下查询,我相信它应该有效:

SELECT checkinout.USERID, checkinout.CHECKTIME, checkinout.CHECKTYPE,checkinout.VERIFYCODE, checkinout.SENSORID, checkinout.Memoinfo, checkinout.WorkCode, checkinout.sn, userinfo.name 
from bio_raw.checkinout
join bio_raw.userinfo
on checkinout.userid = userinfo.userid;
相关问题