MySQL SQL Select 查询多个子 Select
MySQL SQL Select Query with Multiple Sub-Selects
好吧,这个让我抓狂了好几个小时。我觉得我忽略了一些明显的东西。
我有 2 个 tables,service 和 brand
service
-------
id
brand
brand
-----
id
brandName
所以 service.brand 可以是以下任何一个:
Blank
"Other"
Integer (matches brand.id)
String (matches brand.brandName)
String (Not Blank, Not Other, Not brand.brandName)
我正在尝试编写一个查询,从品牌 table 中提取正确的品牌名称,如果 service.brand 的值不在 brand.id 或 brand.brandName,然后显示那里的任何内容。
到目前为止,我的一切正常,除了如果 service.brand 不是空白、不是其他、不在 brand.id 中、不在 brand.brandName 中它不会调出记录。 (我从这里开始称之为 OtherThanOther)。
现在我最近的尝试基本上是正确的,但是 OtherThanOther 字段被拉了很多次,比如如果总记录是 40,OtherThanOther 是同一条记录将近 20 次。帮助!
我最近的尝试..
select
s.*, b.brandName as bname
from
service s, brand b
where
s.brand = b.brandName
or
s.brand = b.id
or
s.brand = 'Other'
or
s.brand = ''
or
(
s.brand not in (select brandName from brand)
and
s.brand not in (select id from brand)
and
s.brand != 'Other'
and
s.brand != ''
)
样本Table数据
service
-------
1 5
2 Dell
3 SomeRandom
4
5 Other
brand
-----
1 HP
2 Gateway
3 Dell
4 Compaq
5 Toshiba
我的查询结果..
(service.id, service.brand, brand.id, brand.brandName, bname)
-------------------------------------------------------------
1 5 5 Toshiba Toshiba
2 Dell 3 Dell Dell
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
4 '', null, null, null
5 Other, null, null, null
我需要它来拉取 SomeRandom 一次,group by 将不起作用,因为可能有多个字段具有与 SomeRandom 相同的值。
非常感谢任何帮助。
谢谢!
您使用的是旧样式inner join
:
from service s, brand b
where s.brand = b.brandName or ...
更清楚的写成:
from service s
inner join brand b on s.brand = b.brandName or ...
在较新的形式中,问题立即突出。 inner join
过滤掉不匹配的行。使用 left join
代替:
from service s
left join brand b on s.brand = b.brandName or ...
现在您将获得所有服务,即使在 brand
table.
中未找到匹配行也是如此
select
s.*,
CASE
WHEN b_id.brandName IS NOT NULL THEN b_id.brandName
WHEN b.brandName IS NOT NULL THEN b.brandName
ELSE s.brand
END as bname
from
service s
LEFT JOIN brand b_id
ON CAST(s.brand AS UNSIGNED) = b_id.id
LEFT JOIN brand b
ON s.brand = b.brandName
您应该 JOIN
2 个具有多个条件的表。像这样:
SELECT s.*, b.brandName AS bname
FROM service AS s
INNER JOIN brand AS b
ON s.brand = b.brandName OR
s.brand = b.id OR
s.brand = 'Other' OR
s.brand = '' OR
(s.brand NOT IN (SELECT brandName FROM brand) AND
s.brand NOT IN (SELECT id FROM brand) AND
s.brand != 'Other' AND
s.brand != '')
第一件事 - 使用 JOIN!
select s.*, b.brandName as bname
from service s
LEFT JOIN brand b ON s.brand = b.brandName or s.brand = b.id
然后你可以添加你的意义建构条件——例如WHERE NOT s.brand IS NULL
您的这些条件导致了您的 table 行彼此相乘的结果:
s.brand = 'Other'
or
s.brand = ''
or
(
s.brand not in (select brandName from brand)
and
s.brand not in (select id from brand)
and
s.brand != 'Other'
and
s.brand != ''
)
好吧,这个让我抓狂了好几个小时。我觉得我忽略了一些明显的东西。
我有 2 个 tables,service 和 brand
service
-------
id
brand
brand
-----
id
brandName
所以 service.brand 可以是以下任何一个:
Blank
"Other"
Integer (matches brand.id)
String (matches brand.brandName)
String (Not Blank, Not Other, Not brand.brandName)
我正在尝试编写一个查询,从品牌 table 中提取正确的品牌名称,如果 service.brand 的值不在 brand.id 或 brand.brandName,然后显示那里的任何内容。
到目前为止,我的一切正常,除了如果 service.brand 不是空白、不是其他、不在 brand.id 中、不在 brand.brandName 中它不会调出记录。 (我从这里开始称之为 OtherThanOther)。
现在我最近的尝试基本上是正确的,但是 OtherThanOther 字段被拉了很多次,比如如果总记录是 40,OtherThanOther 是同一条记录将近 20 次。帮助!
我最近的尝试..
select
s.*, b.brandName as bname
from
service s, brand b
where
s.brand = b.brandName
or
s.brand = b.id
or
s.brand = 'Other'
or
s.brand = ''
or
(
s.brand not in (select brandName from brand)
and
s.brand not in (select id from brand)
and
s.brand != 'Other'
and
s.brand != ''
)
样本Table数据
service
-------
1 5
2 Dell
3 SomeRandom
4
5 Other
brand
-----
1 HP
2 Gateway
3 Dell
4 Compaq
5 Toshiba
我的查询结果..
(service.id, service.brand, brand.id, brand.brandName, bname)
-------------------------------------------------------------
1 5 5 Toshiba Toshiba
2 Dell 3 Dell Dell
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
3 SomeRandom, brand.id, brand.brandName, brand.brandName
4 '', null, null, null
5 Other, null, null, null
我需要它来拉取 SomeRandom 一次,group by 将不起作用,因为可能有多个字段具有与 SomeRandom 相同的值。 非常感谢任何帮助。
谢谢!
您使用的是旧样式inner join
:
from service s, brand b
where s.brand = b.brandName or ...
更清楚的写成:
from service s
inner join brand b on s.brand = b.brandName or ...
在较新的形式中,问题立即突出。 inner join
过滤掉不匹配的行。使用 left join
代替:
from service s
left join brand b on s.brand = b.brandName or ...
现在您将获得所有服务,即使在 brand
table.
select
s.*,
CASE
WHEN b_id.brandName IS NOT NULL THEN b_id.brandName
WHEN b.brandName IS NOT NULL THEN b.brandName
ELSE s.brand
END as bname
from
service s
LEFT JOIN brand b_id
ON CAST(s.brand AS UNSIGNED) = b_id.id
LEFT JOIN brand b
ON s.brand = b.brandName
您应该 JOIN
2 个具有多个条件的表。像这样:
SELECT s.*, b.brandName AS bname
FROM service AS s
INNER JOIN brand AS b
ON s.brand = b.brandName OR
s.brand = b.id OR
s.brand = 'Other' OR
s.brand = '' OR
(s.brand NOT IN (SELECT brandName FROM brand) AND
s.brand NOT IN (SELECT id FROM brand) AND
s.brand != 'Other' AND
s.brand != '')
第一件事 - 使用 JOIN!
select s.*, b.brandName as bname
from service s
LEFT JOIN brand b ON s.brand = b.brandName or s.brand = b.id
然后你可以添加你的意义建构条件——例如WHERE NOT s.brand IS NULL
您的这些条件导致了您的 table 行彼此相乘的结果:
s.brand = 'Other'
or
s.brand = ''
or
(
s.brand not in (select brandName from brand)
and
s.brand not in (select id from brand)
and
s.brand != 'Other'
and
s.brand != ''
)