[英]LEFT JOIN filter certain Null values based on some condition MYSQL
Is it possible to filter certain null values after left join using some conditions in mysql 在mysql中使用某些条件左连接后是否可以过滤某些空值
my sqlfiddle for reference 我的sqlfiddle供参考
http://sqlfiddle.com/#!2/cb03b/1 http://sqlfiddle.com/#!2/cb03b/1
i want to return the tables with their status for a particular booked datetime i have added the date condition but its returning rows of booking of other dates with the status 我想返回特定预订日期时间的状态表,我已经添加了日期条件,但返回的其他日期具有状态的预订行
is my table structure is wrong or is their a solution for it.... 是我的表结构错误还是他们的解决方案...?
expected output for date December, 09 2012 00:00:00+0000 日期的预期输出2012年12月9日00:00:00 + 0000
TABLE_ID FLOOR_ID TABLE_STATUS BOOKING_ID D
1 1 seated 35 December, 09 2012 00:00:00+0000
2 1 free (null) (null)
3 1 free (null) (null)
4 1 free (null) (null)
5 1 free (null) (null)
but i am getting other nulls from booking table 但我从预订表中得到其他空值
TABLE_ID FLOOR_ID TABLE_STATUS BOOKING_ID D
1 1 seated 35 December, 09 2012 00:00:00+0000
2 1 (null) (null)
2 1 (null) (null)
3 1 free (null) (null)
4 1 free (null) (null)
5 1 free (null) (null)
You can use Group By
to do this, but it isn't really clear what you want in the case of multiple matches for one table. 您可以使用“ Group By
依据”来执行此操作,但是对于一个表有多个匹配项的情况,您并不清楚要做什么。 You can use a combination of left outer join
and inner join
to ignore the unwanted booking_table rows: 您可以结合使用left outer join
联接和inner join
联接来忽略不需要的booking_table行:
Select
t.table_id,
t.floor_id,
coalesce(Max(bt.table_status),'free') as table_status,
max(bt.booking_id) as booking_id,
max(bt.date) as d
From
ttable as t
Left Outer Join (
Select
bt.table_id,
bt.table_status,
b.booking_id,
b.date
From
booking_table as bt
Inner Join
booking As b
On b.booking_id = bt.booking_id And b.date = '2012-12-09'
) bt On bt.table_id = t.table_id
Where
t.floor_id = 1
Group By
t.table_id,
t.floor_id
You could use a right outer join
to avoid the nesting, but it's not generally recommended: 您可以使用right outer join
来避免嵌套,但是通常不建议这样做:
Select
t.table_id,
t.floor_id,
coalesce(Max(bt.table_status),'free') as table_status,
max(b.booking_id) as booking_id,
max(b.date) as d
From
booking_table as bt
Inner Join
booking b
On b.booking_id = bt.booking_id And b.date = '2012-12-09'
Right Outer Join
ttable as t
On bt.table_id = t.table_id
Where
t.floor_id = 1
Group By
t.table_id,
t.floor_id
http://sqlfiddle.com/#!2/cb03b/20 http://sqlfiddle.com/#!2/cb03b/20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.