我有entires
,equipments
,brands
,times
和seasons
。
项:
设备:
品牌:
次:
季节:
我的实际SQL查询是:
SELECT entries.*, times.id_season AS id_season
FROM entries, seasons
WHERE entries.time = times.id
但在最后的查询中,我需要下一个我不知道如何获取它的信息:
提前谢谢!
答案 0 :(得分:2)
假设您已将数据标准化。这避免了昂贵的笛卡尔连接。我自己从不使用笛卡尔连接,尽管在某些情况下它们是有用的。不过这里不是。
SELECT
entries.*,
times.id_seasons AS id_season,
b1.name AS equipment_1_name,
b2.name AS equipment_2_name,
seasons.name AS season_name
FROM entries
LEFT JOIN equipments AS equipments_1
ON equipments_1.id = entries.equipment_1
LEFT JOIN brands AS brands_1
ON brands_1.id = equipments_1.id_brand
LEFT JOIN equipments AS equipments_2
ON equipments_2.id = entries.equipment_2
LEFT JOIN brands AS brands_2
ON brands_2.id = equipments_2.id_brand
LEFT JOIN times
ON times.id = entries.time
LEFT JOIN seasons
ON seasons.id = times.id_season;