views:

48

answers:

4

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) FROM Table_1

or is two queries my only option?

+1  A: 

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
effkay
is a UNION more efficient that two separate queries?
Mark
hmmm... I personally think that Union will be 'slightly' better then sending two queries;
effkay
+2  A: 

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.

Kaleb Brasee
`(SELECT MAX(views) FROM Table_1) AS max` !
hsz
Ah yeah, I had the wrong order, thanks hsz.
Kaleb Brasee
It won't be two rows.
Martin
A: 

Use a UNION statement:

SELECT views FROM Table_1 WHERE row_id = 10 
UNION 
SELECT MAX(views) FROM Table_1 
John M
A: 

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

Martin
Only trying to help! Down voting without an explanation is not decent.
Martin
Not me. Thanks for your input.
Mark