我有个问题如下..。
SELECT
contents.id, contents.title, contents.createdBy,
(SELECT userGroup FROM suser_profile WHERE userId =
(SELECT users.id
FROM
users
WHERE
login = contents.createdBy)
) as userGroupID
FROM
contents
WHERE
contents.id > 0
AND contents.contentType = 'News'
**AND userGroupID = 3**
LIMIT 0, 10
当我试图在WHERE子句中分配userGroupID时,SQL会触发一个错误,它在where子句中表示SQL (1054):未知列"userGroupID“
同时,如果我像下面这样做些小改动,
SELECT
contents.id, contents.title, contents.createdBy
FROM
smart_cms_contents
WHERE
contents.id > 0
AND contents.contentType = 'News'
**AND (SELECT userGroup FROM user_profile WHERE userId =
(SELECT users.id
FROM
users
WHERE
users.login = contents.createdBy)
) = 3**
LIMIT 0, 10
然后查询工作正常。
我必须使用多个userGroupID检查,这样,第二个样式将使查询变得更大,我必须有一个类似于第一个样式的样式,任何帮助都值得赞赏。
*注意:表名不是我在项目中使用的原始名称。如果表名中有错误,则可以忽略它。我主要关心的是如何在WHERE子句中使用将变量赋值为的值。
忽略查询中的星星*
发布于 2010-07-18 23:40:11
如果我正确地理解了您的初始查询,那么我相信您想要做的是连接:
SELECT DISTINCT
contents.id, contents.title, contents.createdBy
FROM
contents INNER JOIN users
ON contents.createdBy = users.login
INNER JOIN user_profile
ON user_profile.userId = users.id
WHERE
contents.id > 0
AND contents.contentType = 'News'
AND user_profile.userGroupID = 3
LIMIT 0, 10
发布于 2010-07-18 23:39:46
使用HAVING
。示例:
WHERE
contents.id > 0
AND
contents.contentType = 'News'
HAVING
userGroupID = 3
LIMIT 0, 10
发布于 2010-07-18 23:40:47
完全不是你问的问题的答案,但是.
SELECT DISTINCT c.id, c.title, c.createdBy, s.userGroup AS userGroupID
FROM contents AS c
INNER JOIN users AS u
ON c.createdBy = u.login
INNER JOIN suser_profile AS s
ON s.userId = u.id
WHERE c.id > 0
AND c.contentType = 'News'
AND s.userGroup = 3
https://stackoverflow.com/questions/3279174
复制