跟随SELECTing the contents of the Table, which is a result of another value,我想在生成的字段上保留一个条件。如果我执行此查询:
SELECT *, (
SELECT `TableName` FROM `TableNames` WHERE `TableID`=`IndexType`
) AS `IndexTypeName`,
CASE WHEN `IndexType`=1 THEN (
SELECT `Username` FROM `Users` WHERE `IndexRowID`=`UserID`
) WHEN `IndexType`=2 THEN (
SELECT `MessageContent` FROM `Messages` WHERE `IndexRowID`=`MessageID`
) WHEN `IndexType`=3 THEN (
SELECT `CommentContent` FROM `Comments` WHERE `IndexRowID`=`CommentID`
)
END `TableValue`
FROM `Index`
ORDER BY `IndexTime` DESC;
我显然会得到:
+---------+-----------+------------+---------------------+------------+---------------------+
| IndexID | IndexType | IndexRowID | IndexTime | IndexTable | TableValue |
+---------+-----------+------------+---------------------+------------+---------------------+
| 5 | 2 | 2 | 2015-04-10 11:45:00 | Messages | Hello, Mr. Prave... |
+---------+-----------+------------+---------------------+------------+---------------------+
| 4 | 3 | 1 | 2015-04-10 11:30:00 | Comments | @You, you aren't... |
+---------+-----------+------------+---------------------+------------+---------------------+
| 3 | 2 | 1 | 2015-04-10 11:25:00 | Messages | Thanks for your ... |
+---------+-----------+------------+---------------------+------------+---------------------+
| 2 | 1 | 2 | 2015-04-10 10:55:00 | Users | Jeff Atwood |
+---------+-----------+------------+---------------------+------------+---------------------+
| 1 | 1 | 1 | 2015-04-10 10:50:00 | Users | Praveen Kumar |
+---------+-----------+------------+---------------------+------------+---------------------+
但是,如果我想过滤TableValue
的值,那么如果我提供的查询如下:
SELECT *, (
SELECT `TableName` FROM `TableNames` WHERE `TableID`=`IndexType`
) AS `IndexTypeName`,
CASE WHEN `IndexType`=1 THEN (
SELECT `Username` FROM `Users` WHERE `IndexRowID`=`UserID`
) WHEN `IndexType`=2 THEN (
SELECT `MessageContent` FROM `Messages` WHERE `IndexRowID`=`MessageID`
) WHEN `IndexType`=3 THEN (
SELECT `CommentContent` FROM `Comments` WHERE `IndexRowID`=`CommentID`
)
END `TableValue`
FROM `Index`
WHERE `TableValue` LIKE '%prav%'
ORDER BY `IndexTime` DESC;
我收到以下错误:
查询错误(1054):'where子句'中的未知列'TableValue'。
我不想在子SELECT查询中的每个案例中使用过滤器。是否有其他方法可以通过不执行以下操作:
WHERE
语句中使用SELECT
。提前致谢。
答案 0 :(得分:2)
您无法在fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
中使用别名
您需要使用where clause
或subquery
having clause
或
SELECT *, (
SELECT `TableName` FROM `TableNames` WHERE `TableID`=`IndexType`
) AS `IndexTypeName`,
CASE WHEN `IndexType`=1 THEN (
SELECT `Username` FROM `Users` WHERE `IndexRowID`=`UserID`
) WHEN `IndexType`=2 THEN (
SELECT `MessageContent` FROM `Messages` WHERE `IndexRowID`=`MessageID`
) WHEN `IndexType`=3 THEN (
SELECT `CommentContent` FROM `Comments` WHERE `IndexRowID`=`CommentID`
)
END `TableValue`
FROM `Index`
having `TableValue` LIKE '%prav%'
ORDER BY `IndexTime` DESC;
答案 1 :(得分:2)
您可以使用HAVING
子句来使用别名。
试试这个
SELECT *, (
SELECT `TableName` FROM `TableNames` WHERE `TableID`=`IndexType`
) AS `IndexTypeName`,
CASE WHEN `IndexType`=1 THEN (
SELECT `Username` FROM `Users` WHERE `IndexRowID`=`UserID`
) WHEN `IndexType`=2 THEN (
SELECT `MessageContent` FROM `Messages` WHERE `IndexRowID`=`MessageID`
) WHEN `IndexType`=3 THEN (
SELECT `CommentContent` FROM `Comments` WHERE `IndexRowID`=`CommentID`
)
END `TableValue`
FROM `Index`
HAVING `TableValue` LIKE '%prav%'
ORDER BY `IndexTime` DESC;
答案 2 :(得分:0)
您可以在SQL中使用with子句,您可以在查询的输出中应用过滤器。 它在SQL中使用with调用公共类型表达式。这更有效率。