我的 sql 代码错误在哪里?

2021-12-17 00:00:00 join sql database mysql drupal
'SELECT * FROM t1
          JOIN t2 ON t1.wid = t2.wid
          WHERE t2.wid IS NULL
          LIMIT ' . $number;

这段代码没有任何返回给我你能帮我为什么我不取回值吗?

This code nothing returns to me could you help why i do not take values back??

推荐答案

JOIN t2 ON t1.wid = t1.wid

你是那个意思吗?或者你的意思是t1.wid = t2.wid?在这种情况下,您需要左连接.

did you mean that? or do you really mean t1.wid = t2.wid? in which case you'd want a left join.

编辑

好的,你修好了.除非 t2 中的行的 wid 与 t1 中具有相同 wid 的行匹配,否则不会显示任何结果.

Okay, so you fixed it. That won't show up any results unless there are rows in t2 that have a wid that matches a row in t1 with the same wid.

如果你想要结果,把它改成这样:

If you want results, change it to this:

'SELECT * FROM t1
          LEFT JOIN t2 ON t1.wid = t2.wid
          WHERE t2.wid IS NULL
          LIMIT ' . $number;

下一次编辑

如果目标是使用 t1 中尚未在 t2 中的值来更新 t2,那么它将是这样的:

If the goal is to update t2 with values from t1 that aren't ALREADY in t2, then it would be something like this:

'INSERT INTO t2 
   SELECT t1.* FROM t1
     LEFT JOIN t2 
        ON t1.wid = t2.wid
     WHERE t2.wid IS NULL
     LIMIT ' . $number;

缺少的步骤只是只返回 t1 的结果,然后将它们插入到 t2 中.

The missing step was simply to return only t1's results, and then insert them into t2.

相关文章