SELECT * FROM `product` AS p
JOIN
`store` AS s ON p.prod_id = s.prod_id
我 join 完發現我需要 store的 name, icon 以及 product 的name, icon
這該怎麼辦才好?
我是這樣顯示的:
<? while($row = mysqli_fetch_array($data)){?>
<?=$row['icon'];?>
<?=$row['name'];?>
<?}?>
這樣可以嗎?
SELECT p.prod_id as prod_prod_id, p.icon as prod_icon,
s.prod_id as store_prod_id, s.icon as store_icon
FROM product AS p
JOIN
store AS s ON p.prod_id = s.prod_id
我實在是想破頭
你不是所有的值都要顯示?那當然是要顯示四個值
<?=$row['prod_prod_id'];?>
<?=$row['prod_icon'];?>
<?=$row['store_prod_id'];?>
<?=$row['store_icon'];?>
SELECT p.name as prod_name, p.icon as prod_icon,
s.name as store_name, s.icon as store_icon
FROM product AS p
JOIN
store AS s ON p.prod_id = s.prod_id
<? while($row = mysqli_fetch_array($data)){?>
<?=$row['prod_icon'];?>
<?=$row['prod_name'];?>
<?=$row['store_icon'];?>
<?=$row['store_name'];?>
<?}?>
有什麼好想?
也許你要的是這個
SELECT coalesce(p.name, s.name) as NAME
,coalesce(p.icon, s.icon) as ICON
FROM PRODUCT AS P
JOIN STORE AS S
ON P.PROD_ID = S.PROD_ID