下面的代码提供了太多的结果 事实上,我想列出从不买东西的客户 我该如何修复下面的代码
SELECT
webboard.listweb.id,
webboard.listweb.iditempro,
webboard.listweb.url,
webboard.listweb.useradddate,
webboard.listweb.expiredate,
webboard.prorecord.urlpostonweb
webboard.prorecord.urlpostonweb
FROM
webboard.listweb ,
webboard.prorecord
Where listweb.id Not In
(select webboard.prorecord.idlist From webboard.prorecord )
最佳答案
使用语法
FROM
webboard.listweb ,
webboard.prorecord
将在涉及的表上执行笛卡尔或交叉连接。因此,对于表 listweb
中的每一行,都会显示 prorecord
中的所有行。
您需要使用 INNER JOIN
来仅选择 listweb
中与 prorecord
表中有相关行的行。 prorecord
表中标识行(您的主键)的字段是什么?外键字段的名称是什么?
编辑:只需重新阅读问题和评论,我发现您希望 listweb
中的行没有在预录
您的 SELECT
将如下所示:
SELECT
webboard.listweb.id,
webboard.listweb.iditempro,
webboard.listweb.url,
webboard.listweb.useradddate,
webboard.listweb.expiredate,
webboard.prorecord.urlpostonweb
-- webboard.prorecord.urlpostonweb -- You have this field twice
FROM webboard.listweb LEFT JOIN webboard.prorecord
ON webboard.listweb.id = webboard.prorecord.idlist -- I'm guessing at the foreign key here
WHERE webboard.prorecord.idlist IS NULL
关于mysql - mysql上的sql关于join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4802698/