SQL查询从两个表中选择返回重复的行

时间:2014-10-20 15:29:13

标签: php sql

我正在尝试运行此SQL查询:

SELECT t.*, tu.*, t.contact_name as contact_name, 
t.contact_email as contact_email, t.ticketnumber as ticketnumber, 
t.subject as subject 
FROM tickets t, ticket_updates tu 
WHERE t.ticketnumber = tu.ticketnumber 
AND tu.type = 'update' AND tu.customer <> 'Y' 
AND t.status = 'Awaiting Customer' AND tu.datetime <= '2014-10-18 16:26:00' 
order by tu.datetime DESC LIMIT 0,5

它返回正确的结果,但它显示同一行的多个

2 个答案:

答案 0 :(得分:1)

尝试选择DISTINCT:

SELECT DISTINCT t.*, tu.*...

或者您可以尝试GROUP BY:

SELECT t.*, tu.*, t.contact_name as contact_name, t.contact_email as contact_email, 
 t.ticketnumber as ticketnumber, t.subject as subject 
  FROM tickets t, ticket_updates tu 
  WHERE t.ticketnumber = tu.ticketnumber AND 
  tu.type = 'update' AND tu.customer <> 'Y' AND 
  t.status = 'Awaiting Customer' AND 
   tu.datetime <= '2014-10-18 16:26:00' 
   GROUP BY tu.`id`
   order by tu.datetime DESC LIMIT 0,5

答案 1 :(得分:0)

要快速解决问题,您可以使用SELECT DISTINCT。 但是,通常,重复是提示您的数据库和/或连接条件存在问题。因此,请确保表中没有重复项,并且连接条件是正确的。