我有两个具有以下结构的 MySQL 表:
TABLE `orders`
order_id
order_date
product_id
TABLE `products`
product_id
product_name
product_price
product_type
我想显示表订单中的所有行,如果表产品中有 product_id 则显示该产品的所有数据。如果没有,则仅显示订单表中的所有数据。
我试过这个:
SELECT * FROM orders AS o RIGHT JOIN products AS p ON o.product_id=p.product_id
但是,如果我们在订单表中有一个 product_id 行,而该行在 products 表中不存在,则不会显示来自 orders 的行。无论产品表中是否有 product_id,我都希望显示订单 ID 中的行。如果有 JOIN 它,如果它不只显示来自表订单的数据。
有什么建议么?