如何进行 JOIN 查询 MYSQL?
How to make a JOIN query MYSQL?
我有 2 个 mysql 表:cs_lots
和 cs_lots_history
。相关:id
=id_lot
我需要编写一个查询来显示数组中每批 cs_lots
的 ID,以及来自 cs_lots_history
user_personaname if ID = ID_LOT
查询,我有:
SELECT cs_lots_history.*, cs_lots.id as csid, inv_id, inv_assets, inv_image, inv_color, inv_name, inv_rarity, inv_type, inv_price, price_ticket, places, now_places FROM cs_lots LEFT JOIN cs_lots_history ON cs_lots.id = cs_lots_history.id_lot WHERE active_lot='1' AND user_personaname='@Saundefined' GROUP BY cs_lots.id;
但事实上它只需要 6 手,因为 Table cs_lots_history
Nick@ Saundefined
只需要 6 次购买(唯一 id_lots)。如果他们不这样做,则根本不会打印结果
如果不指定条件WHERE user_personaname - 则获取所有12个手数,但是这样查询就没有意义了,数组不会获取用户购买的这个商品..
我想做类似的事情,显示所有 12 个项目集中在 cs_lots
而如果在 cs_lots_history
中找不到昵称 id
=id_lot
那么结论很简单,一列为NULL。
可能吗?
P.S。如果需要,我的 php 代码.. - :
$rows = array();
$res2 = mysql_query("SELECT cs_lots_history.*, cs_lots.id as csid, inv_id, inv_assets, inv_image, inv_color, inv_name, inv_rarity, inv_type, inv_price, price_ticket, places, now_places FROM cs_lots LEFT JOIN cs_lots_history ON cs_lots.id = cs_lots_history.id_lot WHERE active_lot='1' AND user_steamid='".$_SESSION['steamid']."' GROUP BY cs_lots.id") or die(mysql_error());
while($row = mysql_fetch_array($res2)) {
$rows []= array(
'id' => $row['csid'],
'inv_id' => $row['inv_id'],
'inv_assets' => $row['inv_assets'],
'name' => $row['inv_name'],
'inv_image' => $row['inv_image'],
'inv_rarity' => $row['inv_rarity'],
'inv_color' => $row['inv_color'],
'inv_type' => $row['inv_type'],
'inv_price' => $row['inv_price'],
'price_ticket' => $row['price_ticket'],
'maxUsers' => $row['places'],
'nowUsers' => $row['now_places'],
'my' => !empty($row['id_lot']) ? true : false
);
}
很难理解你在追求什么。我相信您想要查询 returns 来自 cs_lot table 的所有记录,并表明客户是否使用历史记录 table:
从该批次购买
select cs_lots.id, count(h.id) from cs_lots left join cs_lots_history h
on cs_lots.id=h.id_lot and h.personaname='...'
where cs_lots.activelot=1 group by cs_slots.id
如果一个人没有买很多,那么计数将为0。
如果您想要来自两个 table 的全部记录,那么您不应该使用 group by:
select cs_lots.*, h.* from cs_lots left join cs_lots_history h on
cs_lots.id=h.id_lot and h.personaname='...' where cs_lots.activelot=1
这样,如果某个人没有出现在给定批次 ID 的历史记录 table 中,那么历史记录 table 中的列将为空。
我有 2 个 mysql 表:cs_lots
和 cs_lots_history
。相关:id
=id_lot
我需要编写一个查询来显示数组中每批 cs_lots
的 ID,以及来自 cs_lots_history
user_personaname if ID = ID_LOT
查询,我有:
SELECT cs_lots_history.*, cs_lots.id as csid, inv_id, inv_assets, inv_image, inv_color, inv_name, inv_rarity, inv_type, inv_price, price_ticket, places, now_places FROM cs_lots LEFT JOIN cs_lots_history ON cs_lots.id = cs_lots_history.id_lot WHERE active_lot='1' AND user_personaname='@Saundefined' GROUP BY cs_lots.id;
但事实上它只需要 6 手,因为 Table cs_lots_history
Nick@ Saundefined
只需要 6 次购买(唯一 id_lots)。如果他们不这样做,则根本不会打印结果
如果不指定条件WHERE user_personaname - 则获取所有12个手数,但是这样查询就没有意义了,数组不会获取用户购买的这个商品..
我想做类似的事情,显示所有 12 个项目集中在 cs_lots
而如果在 cs_lots_history
中找不到昵称 id
=id_lot
那么结论很简单,一列为NULL。
可能吗?
P.S。如果需要,我的 php 代码.. - :
$rows = array();
$res2 = mysql_query("SELECT cs_lots_history.*, cs_lots.id as csid, inv_id, inv_assets, inv_image, inv_color, inv_name, inv_rarity, inv_type, inv_price, price_ticket, places, now_places FROM cs_lots LEFT JOIN cs_lots_history ON cs_lots.id = cs_lots_history.id_lot WHERE active_lot='1' AND user_steamid='".$_SESSION['steamid']."' GROUP BY cs_lots.id") or die(mysql_error());
while($row = mysql_fetch_array($res2)) {
$rows []= array(
'id' => $row['csid'],
'inv_id' => $row['inv_id'],
'inv_assets' => $row['inv_assets'],
'name' => $row['inv_name'],
'inv_image' => $row['inv_image'],
'inv_rarity' => $row['inv_rarity'],
'inv_color' => $row['inv_color'],
'inv_type' => $row['inv_type'],
'inv_price' => $row['inv_price'],
'price_ticket' => $row['price_ticket'],
'maxUsers' => $row['places'],
'nowUsers' => $row['now_places'],
'my' => !empty($row['id_lot']) ? true : false
);
}
很难理解你在追求什么。我相信您想要查询 returns 来自 cs_lot table 的所有记录,并表明客户是否使用历史记录 table:
从该批次购买select cs_lots.id, count(h.id) from cs_lots left join cs_lots_history h
on cs_lots.id=h.id_lot and h.personaname='...'
where cs_lots.activelot=1 group by cs_slots.id
如果一个人没有买很多,那么计数将为0。
如果您想要来自两个 table 的全部记录,那么您不应该使用 group by:
select cs_lots.*, h.* from cs_lots left join cs_lots_history h on
cs_lots.id=h.id_lot and h.personaname='...' where cs_lots.activelot=1
这样,如果某个人没有出现在给定批次 ID 的历史记录 table 中,那么历史记录 table 中的列将为空。