我有桌子
下面是我必须查询table_order
的SQL,它在输出product_id时非常有效,但我需要它来显示它的名字而不是数字。我需要使用LEFTJOIN
修改mySQL以某种方式连接表product_name
但我一直收到错误。而不是
99 = 15,99 = 16
我需要
99 = name1,99 = name2
SELECT table_order.order_id, table_order.product_id
FROM table_order
WHERE table_order.order_id=99
ORDER BY table_order.order_product_id
table table_order
_________________________
order_id | product_id |
_________________________
99 | 15 |
99 | 16 |
_________________________
table product_name
___________________________
product_id | product_name |
___________________________
15 | name1 |
16 | name2 |
___________________________
答案 0 :(得分:1)
使用
SELECT a.product_id, b.product_name
FROM table_order AS a LEFT JOIN product_name AS b ON a.product_id = b.product_id
WHERE a.order_id = '99'
ORDER BY a.product_id
答案 1 :(得分:0)
Select t1.product_id,t2.product_name from table_order as t1
inner join(
select * from product_name
) as t2
on t1.product_id = t2.product_name
where t1.product_id = //some Number
看看是否有效。
答案 2 :(得分:0)
如您所说,您需要在product_id上加入表格。
SELECT table_order.order_id, product_name.product_name
FROM table_order
INNER JOIN product_name on table_order.product_id = product_name.product_id
WHERE table_order.order_id=99
ORDER BY table_order.product_id
答案 3 :(得分:0)
如果您需要输出所有订单,无论该table_order的产品ID是否与product_table有连接,您都需要使用LEFT JOIN。
SELECT o.order_id, p.product_name
FROM table_order o
LEFT JOIN product_name p ON o.product_id = p.product_id
ORDER BY o.product_id
但是,如果您只需要获取两个表中存在的所有行,则可以使用INNER JOIN
SELECT o.order_id, p.product_name
FROM table_order o
INNER JOIN product_name p ON o.product_id = p.product_id
ORDER BY o.product_id