我有以下sql查询:
SELECT
dbo.Stores.Store_Name,
SUM(tziros_imeras) AS tziros_imeras,
SUM(tziros_imeras_X_fpa) AS tziros_imeras_X_fpa,
SUM(polithenta_karotsia) AS polithenta_karotsia,
SUM(polithenta_kathismata) AS polithenta_kathismata,
SUM(ask_for_furni) AS ask_for_furni,
SUM(ask_for_karotsi) AS ask_for_karotsi,
SUM(ask_for_kathisma) AS ask_for_kathisma,
SUM(furni_reservation) AS furni_reservation,
SUM(tziros_imeras) AS tziros_imeras
FROM
dbo.Reports,
dbo.Stores
WHERE
dbo.Reports.Store_id = dbo.Stores.Store_id
GROUP BY
dbo.Stores.Store_Name
ORDER BY
dbo.Stores.Store_Name ASC
这个查询很完美,他做了我真正需要做的事情(我在循环中得到了理想的结果)。
我的问题是我需要在我的搜索结果中包含更多信息。我想要表格nomisma
中的Nomismata
列信息。
与此相关的表格如下:Stores
& Nomismata
并且它们具有以下结构:
Stores
(Store_id,fpa_id,nomisma_id,Store_Name,Store_password)Nomismata
(nomisma_id,nomisma)是否有任何方法可以在我的查询中正确包含nomisma
信息?
答案 0 :(得分:4)
首先,我建议在“FROM”子句中使用“JOIN”(INNER,LEFT,RIGHT ......):
FROM
dbo.Reports,
dbo.Stores
WHERE
dbo.Reports.Store_id = dbo.Stores.Store_id
GROUP BY
dbo.Stores.Store_Name
ORDER BY
dbo.Stores.Store_Name ASC
到
FROM
dbo.Reports
INNER JOIN
dbo.Stores ON dbo.Reports.Store_id = dbo.Stores.Store_id
GROUP BY
dbo.Stores.Store_Name
ORDER BY
dbo.Stores.Store_Name ASC
...现在,包括你的新表:
SELECT
dbo.Stores.Store_Name,
Nomismata.nomisma,
SUM(tziros_imeras) AS tziros_imeras,
SUM(tziros_imeras_X_fpa) AS tziros_imeras_X_fpa,
SUM(polithenta_karotsia) AS polithenta_karotsia,
SUM(polithenta_kathismata) AS polithenta_kathismata,
SUM(ask_for_furni) AS ask_for_furni,
SUM(ask_for_karotsi) AS ask_for_karotsi,
SUM(ask_for_kathisma) AS ask_for_kathisma,
SUM(furni_reservation) AS furni_reservation,
SUM(tziros_imeras) AS tziros_imeras
FROM
dbo.Reports
INNER JOIN
dbo.Stores ON dbo.Reports.Store_id = dbo.Stores.Store_id
INNER JOIN
Nomismata ON dbo.Stores.nomisma_id = Nomismata.nomisma_id
GROUP BY
dbo.Stores.Store_Name, Nomismata.nomisma
ORDER BY
dbo.Stores.Store_Name ASC
享受!
答案 1 :(得分:1)
您可以在Nomismata
列上添加Stores
表(带nomisma_id
表)的联接。在nomisma
和SELECT
子句中添加GROUP BY
列。由于加入列为nomisma_id
,因此我假设nomisma
是一个维度而不是要求和的数字列。
还添加了JOIN
而不是您拥有的交叉联接。这将使查询更有效。
SELECT
dbo.Stores.Store_Name, dbo.Nomismata.nomisma,
SUM(tziros_imeras) AS tziros_imeras,
SUM(tziros_imeras_X_fpa) AS tziros_imeras_X_fpa,
SUM(polithenta_karotsia) AS polithenta_karotsia,
SUM(polithenta_kathismata) AS polithenta_kathismata,
SUM(ask_for_furni) AS ask_for_furni,
SUM(ask_for_karotsi) AS ask_for_karotsi,
SUM(ask_for_kathisma) AS ask_for_kathisma,
SUM(furni_reservation) AS furni_reservation,
SUM(tziros_imeras) AS tziros_imeras
FROM
dbo.Reports
JOIN dbo.Stores ON dbo.Reports.Store_id = dbo.Stores.Store_id
JOIN dbo.Nomismata ON dbo.Nomismata.nomisma_id = dbo.Stores.nomisma_id
GROUP BY dbo.Stores.Store_Name, dbo.Nomismata.nomisma
ORDER BY dbo.Stores.Store_Name ASC;
答案 2 :(得分:0)
将Nomismata加入现有查询
选择
Stores.Store_Name,
Nomismata.nomisma,
SUM(tziros_imeras)AS tziros_imeras,
SUM(tziros_imeras_X_fpa)AS tziros_imeras_X_fpa,
SUM(polithenta_karotsia)AS polithenta_karotsia,
SUM(polithenta_kathismata)AS polithenta_kathismata,
SUM(ask_for_furni)AS ask_for_furni,
SUM(ask_for_karotsi)AS ask_for_karotsi,
SUM(ask_for_kathisma)AS ask_for_kathisma,
SUM(furni_reservation)AS furni_reservation,
SUM(tziros_imeras)AS tziros_imeras
来自dbo.Reports报告
INNER JOIN dbo.Stores Stores
ON Reports.Store_id = Stores.Store_id
加入dbo.Nomismata Nomismata
ON Nomismata.nomisma_id = Stores.nomisma_id
GROUP BY Stores.Store_Name,
Nomismata.nomisma
ORDER BY Stores.Store_Name