我在将数据库中的表连接起来时遇到了麻烦,我希望以特定的顺序显示它。
表:product
product_id product_price product_qty
1 5.25 100
表:product_detail
product_id product_name product_generic_name
1 Alcohol Isoprophyl Alcohol
我想要JOINING的结果
product_id product_name product_generic_name product_price product_qty
1 Alcohol Isoprophyl Alchohol 5.25 100
我失败的代码是:
SELECT `product_detail`.`product_name`,`product_detail`.`product_name`,`product_detail`.`product_generic_name`
FROM `product_detail`
UNION
SELECT * FROM `product`
但结果不是我需要的,它不会只添加行的列。请给我关于如何解决这个问题的建议,
SELECT p.product_id,
product_name ,
product_detail,
product_generic_name ,
product_price,
product_qty
FROM product as p
inner join product_detail as pd on p.id = pd.id
您可能想要阅读一些关于如何连接表的mysql教程,因为这可能就像获取MySQL Reference manual一样简单
根据您的评论,这是另一种尝试:
Select * from
(SELECT p.product_id,
product_name ,
product_detail,
product_generic_name ,
product_price,
product_qty
FROM product as p) as newTable
或者,如果你想要别的东西,你可以尝试临时表。
create temporary table new_table
SELECT p.product_id,
product_name ,
product_detail,
product_generic_name ,
product_price,
product_qty
FROM product as p
然后在同一个脚本中,或者你调用它,你可以尝试:
从new_table中选择*