从 2 Table 到 Table 1 的列插入

Column Insertion from 2 Table to Table 1

我有 2 个 table:

签到

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

用户信息

Userid | Name | Gender

我只想从用户信息table中获取列名称并将其插入到checkinout table对应于他们的Userid,checkinout.userid=userinfo.userid

以下是我的查询,但我在 userinfo 周围遇到语法错误,你能告诉我我遗漏了什么吗?

Syntax Error: unexpected 'userinfo' (identifier)

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 

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

  • 使用 JOIN 时,无需在 FROM 中提及多个 table 名称。
  • 为了更好的可读性,我添加了 Table 别名 CIUI

工作代码将是:

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

您不能混合使用显式连接和隐式连接:

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 

当您想连接 2 个表时,无需将它们全部放入 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 
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` 

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

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;