查询左连接上的重复项

时间:2013-06-18 12:11:05

标签: php mysql

我在这里有这个问题:

$query_pag_data = "SELECT 
matching.date,
matching.points,
matching.time,
matching.location,
matching.epos_id,
matching.rbpos_id,
matching.time_added,
rbpos_epos.epos_id,
rbpos_epos.rbpos_id,
rbpos_epos.location
FROM
matching
LEFT JOIN
rbpos_epos ON matching.epos_id = rbpos_epos.epos_id AND
matching.rbpos_id = rbpos_epos.rbpos_id WHERE
matching.user_id = ".$id_user." ORDER BY matching.time_added DESC LIMIT ".$start.", ".$per_page;

所以,问题是,我对这部分有问题:

 rbpos_epos ON matching.epos_id = rbpos_epos.epos_id AND matching.rbpos_id = rbpos_epos.rbpos_id

它不会工作,没有任何错误或任何错误,但它不会给出所需的结果。

matching.epos_id / rbpos_id都是varchar类型。 有什么建议吗? 感谢

Matching column
ID  rbpos_id    epos_id
1   123         1234

Rbpos_epos
ID   rbpos_id    epos_id
1    123         1234
2    12          12

我应该得到第一行...因为匹配.rbpos_id和matching.epos_id等于rbpos_epos.rbpos_id和rbpos_epos.epos_id

回声

SELECT matching.date, matching.points, matching.time, matching.location, matching.epos_id, matching.rbpos_id, matching.time_added, rbpos_epos.epos_id, rbpos_epos.rbpos_id, rbpos_epos.location FROM matching LEFT JOIN rbpos_epos ON matching.epos_id = rbpos_epos.epos_id AND matching.rbpos_id = rbpos_epos.rbpos_id WHERE matching.user_id = 4171446415 ORDER BY matching.time_added DESC LIMIT 0, 5

2 个答案:

答案 0 :(得分:0)

为什么不加入一个条件并在条件中过滤数据: -

$query_pag_data = "SELECT 
  m.date,
  m.points,
  m.time,
  m.location,
  m.epos_id,
  m.rbpos_id,
  m.time_added,
  r.epos_id,
  r.rbpos_id,
  r.location
  FROM
  matching as m
  LEFT JOIN
  rbpos_epos as r ON m.epos_id = r.epos_id WHERE
  m.rbpos_id = r.rbpos_id AND
  m.user_id = ".$id_user." ORDER BY m.time_added DESC LIMIT ".$start.", ".$per_page;

答案 1 :(得分:0)

如果您只想检索matching中具有匹配行的rbpos_epos行,则应该仅使用LEFT JOIN(或JOIN)替换INNER JOIN