我有这个......
SELECT
distinct FN.field_first_name_value,
LN.field_last_name_value,
u.mail,
IFNULL(
(SELECT 'Yes'
FROM user_roles SUR, users u
WHERE SUR.uid = u.uid
AND SUR.rid = 3
LIMIT 1
)
,'No') AS 'Paying Member'
FROM
users u,
field_data_field_first_name FN,
field_data_field_last_name LN,
role R,
users_roles SUR
where
u.uid = FN.entity_id
AND
u.uid = LN.entity_id
AND
sur.uid = u.uid
AND
sur.rid = R.rid
但我想在子查询中使用u.uid,以便对每行进行查询。
如何将外部查询中的u.uid包含在子查询中?
谢谢。
答案 0 :(得分:1)
你应该学习一些编写查询的好习惯:
但是,您的问题的答案只是从相关子查询中删除users
表:
coalesce((SELECT 'Yes'
FROM user_roles SUR
WHERE SUR.uid = u.uid AND SUR.rid = 3
LIMIT 1
), 'No') AS `Paying Member`
您的整体查询应该看起来更像这样:
SELECT FN.field_first_name_value, LN.field_last_name_value, u.mail,
COALESCE((SELECT 'Yes'
FROM user_roles SUR
WHERE SUR.uid = u.uid AND SUR.rid = 3
LIMIT 1
), 'No') AS `Paying Member`
FROM users u JOIN
field_data_field_first_name FN
ON u.uid = FN.entity_id JOIN
field_data_field_last_name LN
ON u.uid = LN.entity_id;
roles
和user_roles
表似乎在外部查询中没有用(尽管它们可能用于过滤,我猜这不是这种情况)。