我有这个问题:
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”表的唯一记录,无论连接表中有多少记录< / p>
答案 0 :(得分:2)
如果我明白你的需要。试试这个:
SELECT A.*, LNK.act_id, LNK.remaining, U.username as U_username
FROM anagrafiche A LEFT JOIN lnk_ana-act LNK
ON A.id = LNK.ana_id
LEFT JOIN users U
ON A.uid = U.id
WHERE LNK.act_id <> 57 AND A.closed = '0'
GROUP BY A.id
LIMIT 10