我的查询:
SELECT *
FROM tvn_Listing
LEFT JOIN tvn_ListingOption ON (tvn_Listing.id = tvn_ListingOption.listings_id
AND tvn_ListingOption.options_id = 12)
当我离开加入两个表时如何删除空记录? 我的查询返回的结果不是条件指定的结果。
答案 0 :(得分:0)
如果我正确理解你的问题,问题是:
您可以从tvn_Listing
表中获取NULL
ed table LEFT JOIN
中tvn_ListingOption
个值的记录。那么你可以检查tvn_ListingOption
表中的空值,如:
SELECT *
FROM tvn_Listing
LEFT JOIN tvn_ListingOption ON (tvn_Listing.id = tvn_ListingOption.listings_id
AND tvn_ListingOption.options_id = 12)
WHERE tvn_ListingOption.Id IS NOT NULL
答案 1 :(得分:0)
请注意,添加NOT NULL条件会影响结果(如果options_id为NULLABLE)
test=# select * from tvn_Listing;
id | nm
----+----
1 | A
2 | B
(2 rows)
test=# select * from tvn_listingoption ;
id | op
----+----
1 | A
3 | B
| D
(3 rows)
test=# select * from tvn_listing left join tvn_listingoption on tvn_listing.id=tvn_listingoption.id;
id | nm | id | op
----+----+----+----
1 | A | 1 | A
2 | B | |
(2 rows)
test=# select * from tvn_listing left join tvn_listingoption on tvn_listing.id=tvn_listingoption.id and tvn_listingoption.id is not null ;
id | nm | id | op
----+----+----+----
1 | A | 1 | A
2 | B | |
(2 rows)
test=# select * from tvn_listing left join tvn_listingoption on tvn_listing.id=tvn_listingoption.id where tvn_listingoption.id is not null ;
id | nm | id | op
----+----+----+----
1 | A | 1 | A
(1 row)