I have 2 tables in a database person
and order
tables.
我在一个数据库person和order表中有两个表。
PERSON table:
人表:
PERSON_ID | NAME
ORDER table:
顺序表:
ORDER_ID | ORDER_NO | PERSON_ID
I need to display all the orders + a name of corresponding person if it exists, if not just order details.
我需要显示所有的订单+一个对应的人的名字,如果它存在的话,如果不只是订单的细节。
So far I got up to query:
到目前为止,我一直在查询:
SELECT ORDER_ID, ORDER_NO, order.PERSON_ID, NAME
FROM person, order
WHERE person.PERSON_ID = order.PERSON_ID AND
person.FIRST_NAME IS NOT NULL;
Which gives me orders only if the name is available whereas I need to display all the orders despite the fact if name is available or not.
只有当名称是可用的时候,才给我订单,而我需要显示所有的订单,尽管名称是否可用。
Any suggestions?
有什么建议吗?
1 个解决方案
#1
2
Yes, you can use LEFT JOIN
for that:
是的,你可以使用左连接:
SELECT o.order_id, o.order_no, o.person_id, p.name
FROM `order` o
LEFT JOIN person p
ON p.person_id = o.person_id AND p.FIRST_NAME IS NOT NULL
With LEFT JOIN
if the name
is null
it will still give you the orders.
如果名称为空,那么它仍然会给您订单。
#1
2
Yes, you can use LEFT JOIN
for that:
是的,你可以使用左连接:
SELECT o.order_id, o.order_no, o.person_id, p.name
FROM `order` o
LEFT JOIN person p
ON p.person_id = o.person_id AND p.FIRST_NAME IS NOT NULL
With LEFT JOIN
if the name
is null
it will still give you the orders.
如果名称为空,那么它仍然会给您订单。