Is it possible to write a single query that can return two rows, one specified in a WHERE clause and the other the highest in the table.
For example.
Table_1
-row_id
-views
-content
Is there a query which can combine the following:
SELECT views FROM Table_1 WHERE row_id = 10
SELECT MAX(views) FRO开发者_C百科M Table_1
or is two queries my only option?
You can do this with a subquery:
SELECT (SELECT MAX(views) FROM Table_1) as max_view, views
FROM Table_1
WHERE row_id = 10
Each row would have the same max_view
value in it. I'm not sure what effect this query would have on performance with a large number of rows, however.
I don think it is possible using technically a single query; However, you can use UNION operator
SELECT views FROM Table_1 WHERE row_id = 10
UNION
SELECT MAX(views) FROM Table_1
Use a UNION statement:
SELECT views FROM Table_1 WHERE row_id = 10
UNION
SELECT MAX(views) FROM Table_1
Two rows, one with the selected row and the other with the row with the highest view.
SELECT * FROM Table_1 WHERE row_id = 10 OR views = (SELECT MAX(views) FROM Table_1 LIMIT 1) SORT BY row_id = 10 DESC
精彩评论