子查询从查找中返回了超过 1 个值 table
Subquery returned more than 1 value from look up table
我的查询出现此错误。我查看了 table,其中存储了性别、婚姻状况和客户类型的值。但我想在 asp 中显示全部在 1 个网格视图中。如果数据库中只有 1 条记录,它是可行的,但是当我保存另一条记录时,错误 appears.Can 有人建议修复?
这是我的查询:
SELECT
customer.customer_id
,customer.first_name
,customer.last_name
,customer.birth_date
,customer.phone
,customer.email
,customer.block
,customer.lot
,customer.status
,customer.is_deleted
,(SELECT
lookup_table.value
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
INNER JOIN dbo.customer ON customer.gender = lookup_table.lookup_id) AS Gender
,(SELECT
lookup_table.value
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
INNER JOIN dbo.customer ON customer.marital_status = lookup_table.lookup_id) AS MaritalStatus
,(SELECT
lookup_table.value
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
INNER JOIN dbo.customer ON customer.village = lookup_table.lookup_id) AS Village
,(SELECT
lookup_table.value
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
INNER JOIN dbo.customer ON customer.customer_type = lookup_table.lookup_id) AS CustomerType
FROM dbo.customer
提前致谢!
你有这个错误是有道理的。有一条记录一切都很好,但是你的子查询有更多的值和更多的记录,引擎不知道该选择什么。
因为您根本没有过滤,所以您希望所有客户都具有他们的特征(性别,村庄......)
我相信你应该做这样的事情:
SELECT
customer.customer_id
,customer.first_name
,customer.last_name
,customer.birth_date
,customer.phone
,customer.email
,customer.block
,customer.lot
,customer.status
,customer.is_deleted
,Gender
,marital_status
,village
,customer_type
FROM dbo.customer a
inner join
(SELECT
lookup_table.value as Gender, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS Gender on a.gender = Gender.lookup_id
inner join
(SELECT
lookup_table.value as marital_status, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS marital_status on a.marital_status = marital_status.lookup_id
inner join
(SELECT
lookup_table.value as village, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS village on a.village = village.lookup_id
inner join
(SELECT
lookup_table.value as customer_type, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS customer_type on a.customer_type = customer_type.lookup_id
如@zip 所述,子查询 returns 多行并产生问题,您可以尝试这样的操作并应用过滤器来消除重复项
SELECT
customer.customer_id
,customer.first_name
,customer.last_name
,customer.birth_date
,customer.phone
,customer.email
,customer.block
,customer.lot
,customer.status
,customer.is_deleted
,gender.value AS Gender
,MaritalStatus.value AS MaritalStatus
,Village.value AS Village
,CustomerType.value AS CustomerType
FROM dbo.customer
LEFT OUTER JOIN dbo.lookup_table gender ON customer.gender = gender.lookup_id
LEFT OUTER JOIN dbo.lookup_table MaritalStatus ON customer.marital_status = MaritalStatus.lookup_id
LEFT OUTER JOIN dbo.lookup_table Village ON customer.village = Village.lookup_id
LEFT OUTER JOIN dbo.lookup_table CustomerType ON customer.customer_type = CustomerType.lookup_id
我听从了 zip 的回答。他让客户 table 成为 'a'。用 'a' 替换了客户并删除了性别,marital_status,村庄,customer_type。
这里是查询:
SELECT
a.customer_id
,a.first_name
,a.last_name
,a.birth_date
,a.phone
,a.email
,a.block
,a.lot
,a.status
,a.is_deleted
FROM dbo.customer a
inner join
(SELECT
lookup_table.value as Gender, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS Gender on a.gender = Gender.lookup_id
inner join
(SELECT
lookup_table.value as marital_status, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS marital_status on a.marital_status = marital_status.lookup_id
inner join
(SELECT
lookup_table.value as village, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS village on a.village = village.lookup_id
inner join
(SELECT
lookup_table.value as customer_type, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS customer_type on a.customer_type = customer_type.lookup_id
我的查询出现此错误。我查看了 table,其中存储了性别、婚姻状况和客户类型的值。但我想在 asp 中显示全部在 1 个网格视图中。如果数据库中只有 1 条记录,它是可行的,但是当我保存另一条记录时,错误 appears.Can 有人建议修复?
这是我的查询:
SELECT
customer.customer_id
,customer.first_name
,customer.last_name
,customer.birth_date
,customer.phone
,customer.email
,customer.block
,customer.lot
,customer.status
,customer.is_deleted
,(SELECT
lookup_table.value
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
INNER JOIN dbo.customer ON customer.gender = lookup_table.lookup_id) AS Gender
,(SELECT
lookup_table.value
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
INNER JOIN dbo.customer ON customer.marital_status = lookup_table.lookup_id) AS MaritalStatus
,(SELECT
lookup_table.value
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
INNER JOIN dbo.customer ON customer.village = lookup_table.lookup_id) AS Village
,(SELECT
lookup_table.value
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
INNER JOIN dbo.customer ON customer.customer_type = lookup_table.lookup_id) AS CustomerType
FROM dbo.customer
提前致谢!
你有这个错误是有道理的。有一条记录一切都很好,但是你的子查询有更多的值和更多的记录,引擎不知道该选择什么。 因为您根本没有过滤,所以您希望所有客户都具有他们的特征(性别,村庄......) 我相信你应该做这样的事情:
SELECT
customer.customer_id
,customer.first_name
,customer.last_name
,customer.birth_date
,customer.phone
,customer.email
,customer.block
,customer.lot
,customer.status
,customer.is_deleted
,Gender
,marital_status
,village
,customer_type
FROM dbo.customer a
inner join
(SELECT
lookup_table.value as Gender, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS Gender on a.gender = Gender.lookup_id
inner join
(SELECT
lookup_table.value as marital_status, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS marital_status on a.marital_status = marital_status.lookup_id
inner join
(SELECT
lookup_table.value as village, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS village on a.village = village.lookup_id
inner join
(SELECT
lookup_table.value as customer_type, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS customer_type on a.customer_type = customer_type.lookup_id
如@zip 所述,子查询 returns 多行并产生问题,您可以尝试这样的操作并应用过滤器来消除重复项
SELECT
customer.customer_id
,customer.first_name
,customer.last_name
,customer.birth_date
,customer.phone
,customer.email
,customer.block
,customer.lot
,customer.status
,customer.is_deleted
,gender.value AS Gender
,MaritalStatus.value AS MaritalStatus
,Village.value AS Village
,CustomerType.value AS CustomerType
FROM dbo.customer
LEFT OUTER JOIN dbo.lookup_table gender ON customer.gender = gender.lookup_id
LEFT OUTER JOIN dbo.lookup_table MaritalStatus ON customer.marital_status = MaritalStatus.lookup_id
LEFT OUTER JOIN dbo.lookup_table Village ON customer.village = Village.lookup_id
LEFT OUTER JOIN dbo.lookup_table CustomerType ON customer.customer_type = CustomerType.lookup_id
我听从了 zip 的回答。他让客户 table 成为 'a'。用 'a' 替换了客户并删除了性别,marital_status,村庄,customer_type。
这里是查询:
SELECT
a.customer_id
,a.first_name
,a.last_name
,a.birth_date
,a.phone
,a.email
,a.block
,a.lot
,a.status
,a.is_deleted
FROM dbo.customer a
inner join
(SELECT
lookup_table.value as Gender, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS Gender on a.gender = Gender.lookup_id
inner join
(SELECT
lookup_table.value as marital_status, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS marital_status on a.marital_status = marital_status.lookup_id
inner join
(SELECT
lookup_table.value as village, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS village on a.village = village.lookup_id
inner join
(SELECT
lookup_table.value as customer_type, lookup_id
FROM dbo.lookup_description
INNER JOIN dbo.lookup_table
ON lookup_description.desc_id = lookup_table.group_id
) AS customer_type on a.customer_type = customer_type.lookup_id