这是我的代码:
select a.id_brg, a.nm_brg, a.jen_sat, a.nm_kat,a.stok,a.laku, (a.stok-a.laku) as difference from (SELECT barang.id_brg, barang.nm_brg, jen_sat, nm_kat, SUM( IFNULL( stok_brg.stok, 0 ) ) AS stok, IFNULL( laku, 0 ) AS laku
FROM barang
JOIN satuan ON barang.id_sat = satuan.id_sat
JOIN kategori ON barang.id_kat = kategori.id_kat
LEFT JOIN stok_brg ON barang.id_brg = stok_brg.id_brg
LEFT JOIN (
SELECT barang.id_brg, SUM( IFNULL( brg_laku.dibeli, 0 ) ) AS laku
FROM barang, brg_laku
WHERE barang.id_brg = brg_laku.id_brg
GROUP BY barang.id_brg ASC
) AS brg_laku ON barang.id_brg = brg_laku.id_brg
GROUP BY barang.id_brg ASC) as a;
如何在该代码中添加where
?
where difference <= 3
每次写where
时,都会收到错误消息:
未知列&#39;差异&#39;在where子句
答案 0 :(得分:0)
尝试以下:
Select * from (select a.id_brg, a.nm_brg, a.jen_sat, a.nm_kat,a.stok,a.laku, (a.stok-a.laku) as difference from (SELECT barang.id_brg, barang.nm_brg, jen_sat, nm_kat, SUM( IFNULL( stok_brg.stok, 0 ) ) AS stok, IFNULL( laku, 0 ) AS laku
FROM barang
JOIN satuan ON barang.id_sat = satuan.id_sat
JOIN kategori ON barang.id_kat = kategori.id_kat
LEFT JOIN stok_brg ON barang.id_brg = stok_brg.id_brg
LEFT JOIN (
SELECT barang.id_brg, SUM( IFNULL( brg_laku.dibeli, 0 ) ) AS laku
FROM barang, brg_laku
WHERE barang.id_brg = brg_laku.id_brg
GROUP BY barang.id_brg ASC
) AS brg_laku ON barang.id_brg = brg_laku.id_brg
GROUP BY barang.id_brg ASC) as a;select a.id_brg, a.nm_brg, a.jen_sat, a.nm_kat,a.stok,a.laku, (a.stok-a.laku) as difference from (SELECT barang.id_brg, barang.nm_brg, jen_sat, nm_kat, SUM( IFNULL( stok_brg.stok, 0 ) ) AS stok, IFNULL( laku, 0 ) AS laku
FROM barang
JOIN satuan ON barang.id_sat = satuan.id_sat
JOIN kategori ON barang.id_kat = kategori.id_kat
LEFT JOIN stok_brg ON barang.id_brg = stok_brg.id_brg
LEFT JOIN (
SELECT barang.id_brg, SUM( IFNULL( brg_laku.dibeli, 0 ) ) AS laku
FROM barang, brg_laku
WHERE barang.id_brg = brg_laku.id_brg
GROUP BY barang.id_brg ASC
) AS brg_laku ON barang.id_brg = brg_laku.id_brg
GROUP BY barang.id_brg ASC) as a)fq where difference <= 3
答案 1 :(得分:0)
SELECT a.id_brg,
a.nm_brg,
a.jen_sat,
a.nm_kat,
a.stok,
a.laku,
(a.stok-a.laku) as difference
FROM (
SELECT barang.id_brg,
barang.nm_brg,
jen_sat,
nm_kat,
SUM(IFNULL(stok_brg.stok, 0)) AS stok,
IFNULL(laku, 0) AS laku
FROM barang
JOIN satuan ON barang.id_sat = satuan.id_sat
JOIN kategori ON barang.id_kat = kategori.id_kat
LEFT JOIN stok_brg ON barang.id_brg = stok_brg.id_brg
LEFT JOIN (
SELECT barang.id_brg,
SUM(IFNULL( brg_laku.dibeli, 0 )) AS laku
FROM barang
INNER JOIN brg_laku
ON barang.id_brg = brg_laku.id_brg
GROUP BY barang.id_brg ASC
) AS brg_laku
ON barang.id_brg = brg_laku.id_brg
GROUP BY barang.id_brg ASC
) as a
WHERE (a.stok-a.laku) <= 3;
现在,你能在MySQL的WHERE子句中使用别名吗?
来自MySQL手册:
不允许在WHERE子句中引用列别名 因为WHERE时可能尚未确定列值 子句被执行。见B.1.5.4,“Problems with Column Aliases”。
答案 2 :(得分:0)
您无法在声明它的同一SELECT
内引用列别名。您可以按列表达式(a.stok-a.laku
)进行过滤,也可以将其嵌套为子查询,并在最外层范围内对其进行过滤。
按列表达式过滤:
select
a.id_brg,
a.nm_brg,
a.jen_sat,
a.nm_kat,
a.stok,
a.laku,
(a.stok-a.laku) as difference
from
(SELECT
barang.id_brg,
barang.nm_brg,
jen_sat,
nm_kat,
SUM( IFNULL( stok_brg.stok, 0 ) ) AS stok, IFNULL( laku, 0 ) AS laku
FROM barang
JOIN satuan ON barang.id_sat = satuan.id_sat
JOIN kategori ON barang.id_kat = kategori.id_kat
LEFT JOIN stok_brg ON barang.id_brg = stok_brg.id_brg
LEFT JOIN (
SELECT
barang.id_brg,
SUM( IFNULL( brg_laku.dibeli, 0 ) ) AS laku
FROM
barang, brg_laku
WHERE
barang.id_brg = brg_laku.id_brg
GROUP BY
barang.id_brg ASC
) AS brg_laku
ON barang.id_brg = brg_laku.id_brg
GROUP BY
barang.id_brg ASC) as a
WHERE
a.stok-a.laku <= 3 -- Here!
嵌套子查询:
SELECT
X.*
FROM
(
select
a.id_brg,
a.nm_brg,
a.jen_sat,
a.nm_kat,
a.stok,
a.laku,
(a.stok-a.laku) as difference
from
(SELECT
barang.id_brg,
barang.nm_brg,
jen_sat,
nm_kat,
SUM( IFNULL( stok_brg.stok, 0 ) ) AS stok, IFNULL( laku, 0 ) AS laku
FROM barang
JOIN satuan ON barang.id_sat = satuan.id_sat
JOIN kategori ON barang.id_kat = kategori.id_kat
LEFT JOIN stok_brg ON barang.id_brg = stok_brg.id_brg
LEFT JOIN (
SELECT
barang.id_brg,
SUM( IFNULL( brg_laku.dibeli, 0 ) ) AS laku
FROM
barang, brg_laku
WHERE
barang.id_brg = brg_laku.id_brg
GROUP BY
barang.id_brg ASC
) AS brg_laku
ON barang.id_brg = brg_laku.id_brg
GROUP BY
barang.id_brg ASC) as a
) AS X
WHERE
X.difference <= 3 -- Here!
答案 3 :(得分:0)
使用WHERE (a.stok-a.laku) <= 3
而不是别名。
SELECT a.id_brg, a.nm_brg, a.jen_sat, a.nm_kat,a.stok,a.laku, (a.stok-a.laku) difference FROM(SELECT B.id_brg, B.nm_brg, jen_sat, nm_kat, SUM( IFNULL(SB.stok, 0 )) stok, IFNULL(laku, 0) laku
FROM barang B
JOIN satuan S ON B.id_sat = S.id_sat
JOIN kategori ON barang.id_kat = kategori.id_kat
LEFT JOIN stok_brg SB ON B.id_brg = SB.id_brg
LEFT JOIN (SELECT B.id_brg, SUM( IFNULL(BL.dibeli, 0)) laku
FROM barang B
INNER JOIN brg_laku BL ON B.id_brg = BL.id_brg
WHERE (a.stok-a.laku) <= 3
GROUP BY B.id_brg ASC) brg_laku BL ON B.id_brg = BL.id_brg
GROUP BY B.id_brg ASC) a;
答案 4 :(得分:0)
你必须首先获得a.stok和a.laku,计算差异,然后将其存储到sql变量中,然后在where子句中使用它。因为mysql不会在select情况下首先计算差异,然后提取相应的行。你必须这样做。我希望这能帮到你
答案 5 :(得分:0)
SELECT
statement的文档说明:
可以使用
select_expr
为AS alias_name
提供别名。别名用作表达式的列名,可以在GROUP BY
,ORDER BY
或HAVING
子句中使用。[...]
不允许在
WHERE
子句中引用列别名,因为在执行WHERE
子句时可能尚未确定列值。
请参阅Section B.5.4.4, "Problems with Column Aliases"。
解决方案很简单:不要在WHERE
子句中使用别名,而是使用别名表达式:
SELECT ... (a.stok-a.laku) as difference, ...
...
WHERE a.stok-a.laku <= 3
...
答案 6 :(得分:0)
MySQL - 与所有其他数据库一样 - 不允许WHERE
子句中的表别名。但是,MySQL扩展了HAVING
子句以允许这种过滤。所以,你可以这样做:
SELECT a.id_brg, a.nm_brg, a.jen_sat, a.nm_kat, a.stok, a.laku,
(a.stok - a.laku) as difference
FROM (SELECT barang.id_brg, barang.nm_brg, jen_sat, nm_kat,
SUM(COALESCE(stok_brg.stok, 0 )) AS stok,
COALESCE( laku, 0 ) AS laku
FROM barang JOIN
satuan
ON barang.id_sat = satuan.id_sat JOIN
kategori
ON barang.id_kat = kategori.id_kat LEFT JOIN
stok_brg
ON barang.id_brg = stok_brg.id_brg LEFT JOIN
(SELECT brg_laku.id_brg, SUM(COALESCE(brg_laku.dibeli, 0)) AS laku
FROM brg_laku
GROUP BY brg_laku.id_brg
) brg_lak
ON barang.id_brg = brg_laku.id_brg
GROUP BY barang.id_brg
) as a
HAVING difference <= 3;
注意:
COALESCE()
到IFNULL()
,因为前者是ANSI标准函数。JOIN
似乎没必要。 (可能在某些情况下,但我猜它不是。)COALESCE()
你可能不需要IFNULL()
/ SUM()
。我把它们留在了,但聚合函数中忽略了NULL
。