【问题标题】:Left join using a condition使用条件左连接
【发布时间】:2013-01-04 11:01:21
【问题描述】:

我有这个问题。

我需要从匹配表中获取这些字段:

date, points

然后我的匹配表中还有一个epos_id 字段..

我还有另一个 rbpos_epos 表,其中包含 epos_idlocation 字段。

我需要使用连接从rbpos_epos 获取location .. 像这样:

SELECT matching.date, matching.points, matching.time,matching.location,matching.epos_id,rbpos_epos.epos_id,rbpos_epos.location
FROM matching  WHERE matching.user_id="'.$id_user.'"
LEFT JOIN rbpos_epos where matching.epos_id=rbpos_epos.epos_id; 

【问题讨论】:

    标签: mysql select join left-join


    【解决方案1】:
    SELECT 
        first_table.date,
        first_table.points,
        first_table.time,
        first_table.location,
        first_table.epos_id,
        rbpos_epos.epos_id,
        rbpos_epos.location
    FROM
        first_table
    LEFT JOIN
        rbpos_epos ON first_table.epos_id = rbpos_epos.epos_id
    WHERE
        first_table.user_id = "'.$id_user.'";
    

    【讨论】:

    • 检查location字段是否为空。
    • 谢谢你,你是对的,我很抱歉造成混乱:) 我接受这个作为答案:)
    【解决方案2】:

    使用 on 代替 where -

        SELECT matching.date, matching.points, matching.time,matching.location,matching.epos_id,rbpos_epos.epos_id,rbpos_epos.location
       FROM matching LEFT JOIN rbpos_epos ON matching.epos_id=rbpos_epos.epos_id      
          WHERE matching.user_id="'.$id_user.'";
    

    【讨论】:

    • 这是什么?复制粘贴?
    【解决方案3】:

    试试这个:

    SELECT m.date, m.points, m.time,m.location,m.epos_id,re.epos_id,re.location
    FROM matching  m 
    LEFT JOIN rbpos_epos re ON m.epos_id=re.epos_id
    WHERE m.user_id= 10
    

    【讨论】:

    • 有什么问题。添加示例表数据
    猜你喜欢
    • 2022-11-10
    • 2012-12-22
    • 2016-03-10
    • 2015-07-18
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多