JOIN 字段不等于值的表
我有这个查询:
SELECT `A`.*, `LNK`.`act_id`, `LNK`.`remaining`, `U`.`username` as U_username
FROM (`anagrafiche` as A)
LEFT JOIN `lnk_ana-act` AS LNK ON `A`.`id` = `LNK`.`ana_id`
LEFT JOIN `users` AS U ON `A`.`uid` = `U`.`id`
WHERE (`LNK`.`act_id` != 57 OR `LNK`.`act_id` IS NULL) AND A.closed = '0'
LIMIT 10
此查询选择 act_id 不为 57 的所有字段,导致第一个表“anagrafiche”有多个重复项。这样做显然是错误的,这种情况我该怎么办?
感谢
编辑澄清。
无论有多少 lnk_ana-act 记录,我只想要第一个表的一条记录。更准确地说,lnk_ana-act 表中有数千条记录不等于 act_id
!= 57,但我只需要“anagrafiche”表的一条唯一记录,无论该表中有多少条记录连接表
I have this query:
SELECT `A`.*, `LNK`.`act_id`, `LNK`.`remaining`, `U`.`username` as U_username
FROM (`anagrafiche` as A)
LEFT JOIN `lnk_ana-act` AS LNK ON `A`.`id` = `LNK`.`ana_id`
LEFT JOIN `users` AS U ON `A`.`uid` = `U`.`id`
WHERE (`LNK`.`act_id` != 57 OR `LNK`.`act_id` IS NULL) AND A.closed = '0'
LIMIT 10
This query selects ALL fields where act_id is not 57, resulting on several duplicates of the first table "anagrafiche". It's obviously wrong to do it like this, what should I do in this case?
Thanks
EDIT CLARIFICATION.
I want only one record of the first table regardless of how many lnk_ana-act records there are. To be more precise there are thousands of records lnk_ana-act table that are not equal to act_id
!= 57, but I only need one unique record of the "anagrafiche" table regardless of how many records in the joined table
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我明白你需要什么。试试这个:
If I understand what you need. try this: