临时订购表
temp_id series_id news_ticket_breakdown_id quantity expiry_time
58db09bba25b4 2 2 2 1490750973
58db09bba25b4 2 4 1 1490750973
新闻资讯表
news_id series_id news_title
26 2 GENERIC TICKETING
27 2 GENERIC TICKETING
当我在series_id上进行左连接时,我返回4行,我只想要2.我不确定如何将连接的行数限制为一行。
CURRENT QUERY
SELECT
*
FROM
ticket_ordering_temp
LEFT JOIN
news_items
on
( news_items.series_id = SELECT
DISTINCT(event_id)
FROM
news_items
where
series_id=ticket_ordering_temp.series_id
)
where
temp_id='58db09bba25b4''
期望的结果 我只想从表2中获取1行,而不是如上所述连接的2行
temp_id series_id news_ticket_breakdown_id quantity expiry_time news_title
58db09bba25b4 2 2 2 1490750973 GENERIC TICKETING
58db09bba25b4 2 4 1 1490750973 GENERIC TICKETING
答案 0 :(得分:2)
对于您的样本数据和您想要的结果,您可以尝试按照以下步骤进行操作:
SELECT
ticket_ordering_temp.*, t.news_title
FROM
ticket_ordering_temp
LEFT JOIN(
SELECT DISTINCT series_id, news_title
FROM news_items
) t ON ticket_ordering_temp.series_id = t.series_id
WHERE temp_id = '58db09bba25b4'
但是,我认为你应该告诉我们这些样本数据背后的逻辑。
答案 1 :(得分:0)
尝试使用tempTable进行查询:
select distinct * from
(select * from
(select '58db09bba25b4' as temp_id, 2 as series_id,2 as news_ticket_breakdown_id,2 as quantity,1490750973 as expiry_time union all
select '58db09bba25b4' , 2 , 4 , 1, 1490750973) as tbl_order) as tbl_order
left join
(select series_id,news_title from
(select 26 as news_id, 2 as series_id, 'GENERIC TICKETING' as news_title union all
select 27 , 2 , 'GENERIC TICKETING') as tbl_items) tbl_items
on tbl_order.series_id = tbl_items.series_id where temp_id ='58db09bba25b4'
使用实际表格查询:
select DISTINCT * from
(select temp_id,series_id,news_ticket_breakdown_id,quantity,expiry_time from tbl_order) as tb_order
left join
(select series_id,news_title from tbl_items) as tb_items
on tb_order.series_id = tb_items.series_id where temp_id ='58db09bba25b4'
结果:
58db09bba25b4 2 2 2 1490750973 2 GENERIC TICKETING
58db09bba25b4 2 4 1 1490750973 2 GENERIC TICKETING
只是提供另一种解决方案。