0

嘿伙计们,我有这个查询:

SELECT Schwellwert
FROM lm_Schwellwert
WHERE (Typ = (SELECT Typ FROM lm_Artikel WHERE `Index` = (SELECT DISTINCT `Index` FROM lm_Warenkorb)))

但是当我运行它时,我收到了这个错误:

1242 - 子查询返回多于 1 行

我不知道我的查询中有什么错误。

示例lm_Schwellwert外观:

Typ     Schwellwert
Computer    78

示例lm_Artikel外观:

Index   Artikelbezeichnung  Status  Bestand     Lieferant    Typ
1           HP Elitebook        OK      19         HP         Computer     

示例lm_Warenkorb外观:

Index
11
1
4

5 回答 5

2

where 条件只能在一个 vale 中运行(所以 x=y 等等)
在你的情况下,你的子查询

(SELECT Typ FROM lm_Artikel WHERE索引= (SELECT DISTINCT索引FROM lm_Warenkorb))

返回不止一行,这会导致错误。

尝试修改您的查询如下

SELECT Schwellwert
FROM lm_Schwellwert
WHERE (Typ IN (SELECT Typ FROM lm_Artikel WHERE `Index` IN (SELECT DISTINCT `Index` FROM lm_Warenkorb)))

运算符将IN帮助您,因为接受一组值并Typ在等于其中一个值时返回 true

于 2012-11-22T08:05:33.520 回答
1

试试这个查询

Select 
    lm_Schwellwert.Schwellwert
From lm_Schwellwert
Left join lm_Schwellwert as r 
    on r.Typ = (SELECT Typ FROM lm_Artikel WHERE `Index` = (SELECT DISTINCT `Index` FROM lm_Warenkorb))
Where r.id is not null  
group by lm_Schwellwert.Typ
于 2012-11-22T08:11:26.077 回答
1

也许in会帮助你:

SELECT Schwellwert
FROM lm_Schwellwert
WHERE (Typ in (SELECT Typ FROM lm_Artikel WHERE `Index` in (SELECT DISTINCT `Index` FROM lm_Warenkorb)))

您还可以使用聚合函数min,例如:

SELECT Schwellwert
FROM lm_Schwellwert
WHERE (Typ in (SELECT min(Typ) FROM lm_Artikel WHERE `Index` in (SELECT min(`Index`) FROM lm_Warenkorb)))
于 2012-11-22T08:05:10.283 回答
1

尝试这个

SELECT Schwellwert
FROM lm_Schwellwert
WHERE (Typ = (SELECT Typ FROM lm_Artikel 
              WHERE `Index` in (SELECT DISTINCT `Index` FROM lm_Warenkorb)))

因为你在索引中有两个值

于 2012-11-22T08:05:14.117 回答
1
SELECT DISTINCT `Index` FROM lm_Warenkorb

此子查询返回两行..它应该只有一个,因为您使用等号..

尝试这个:

SELECT Schwellwert
FROM lm_Schwellwert
WHERE (Typ = (SELECT  Typ FROM lm_Artikel WHERE `Index` in (SELECT DISTINCT `Index` FROM lm_Warenkorb)) limit 1)
于 2012-11-22T08:05:30.930 回答