I know MySQL doesn't have a TOP 1 function for selecting the the first row. So I use limit 1 to do it. But how can I select the first row from two tables (tableX, tableY) at the same time.
like this?
SELECT * FROM Database.tableX
limit 1, Database.tableY limit 1
Probably, if the table definitions match:
SELECT * FROM Database.tableX LIMIT 1
UNION ALL
(
SELECT * FROM Database.tableY LIMIT 1
)
Note the inner bracket is necessary. Else the optimizer will limit the result set to 1 seeing the last LIMIT 1 clause
This is not possible unless there is some kind of relation between the two rows.
The other answers here try to join two rows, but they give no guarantee that it is the first row in each table. They also assume there is a field that can be matched from the two tables.
My best suggestion is to do two queries:
SELECT * FROM Database.tableX limit 1;
SELECT * FROM Database.tableY limit 1;
EDIT: you should also add a ORDER BY to tell MySQL how to sort the rows.
Just this:
SELECT *
FROM Database.TableX, Database.TableY
LIMIT 1
or maybe this?
SELECT *
FROM
(SELECT * FROM Database.TableX LIMIT 1) t1,
(SELECT * FROM Database.TableY LIMIT 1) t2
Related
I was wondering how to grab the first item and the last item in a MySQL database. I know these two lines will do the work, but I am trying to limit it down to one line.
SELECT * FROM `test_table` order by ID asc limit 0,1
SELECT * FROM `test_table` order by ID desc limit 0,1
For example there is a table called test_table and it has 5 rows. I would like to get row number 1 and row number 5 in the same statement. This will save me from using many PHP if statements and will shorten up my code. Any help would be appreciated.
The following SQL does not get the first item:
SELECT * FROM `test_table` limit 0,1
It returns an arbitrary row. SQL tables and result sets are unordered, so limit without order by returns an indeterminate row.
The syntax to do what you want is:
(SELECT t.* FROM `test_table` t ORDER BY id ASC LIMIT 1)
UNION ALL
(SELECT t.* FROM `test_table` t ORDER BY id DESC LIMIT 1);
Note: If the table has only one row, this returns two rows, because the same row is the "first" and "last". If you want only one in this situation, you could use UNION rather than UNION ALL.
use union it will combine both sql queries and you the expected result
SELECT * FROM `test_table` limit 0,1
union
SELECT * FROM `test_table` order by ID desc limit 0,1
You can use the min and max ID if it's monotonically increasing -
SELECT * FROM `test_table`
WHERE ID IN ((SELECT MIN(ID) FROM `test_table`), (SELECT MAX(ID) FROM `test_table`));
I'd suggest running the explain for the query to see if it performs as per your expectation.
I want to have a union of two request BUT the order by rand() of the second one doesn't work..
(select * from survey
where is_priority = 1)
union all (
select * from (
select * from survey
order by rand()
) as t );
The result look like this :
I speculate that you want is_priority = 1 first followed by the rest in random order.
If so, you should not do this with union all. Just add the right keys to the order by:
select s.*
from survey s
order by (s.is_priority = 1) desc, -- put priority 1 first in the result set
rand();
Admittedly, this puts the top priority rows in random order (amongst them). But you haven't specified an order for them (and this is a non-issue if there is only one row where the priority condition is true).
Not really sure how to do this, but is it possible in one query to fetch x amount of records from a table, and if not enough is found, it will just randomly select duplicates.
I have a photos table, let's say it has 5 records in it, and I want to pull out 10 records and order them randomly, so I have something like:
SELECT * FROM TABLE
ORDER BY RAND()
LIMIT 10
This will just pull back 5 randomly, cos that is all I have in the table. Can I tell MySQL, hey, if you find less than 10, just randomly grab more until you reach that number?
Any help appreciated!
Thanks
This will do it:
select * from Table1
union all
select * from
(
select * from
(
select * from Table1 limit 10
union all
select * from Table1 limit 10
union all
select * from Table1 limit 10
union all
select * from Table1 limit 10
-- more unions...
) t2 order by rand()
) rand_ordered
limit 10
Union the table for as many times as your number of needed records is (10 times in this example) to make it work with only one row in the table, order the result by rand() and append it to your table with another union all.
This might not be the best performing solution tho, but it will do it.
Example here: SQLFIDDLE
how to form a query to select 'm' rows randomly from a query result which has 'n' rows.
for ex; 5 rows from a query result which has 50 rows
i try like as follows but it errors
select * from (select * from emp where alphabet='A' order by sal desc) order by rand() limit 5;
u can wonder that why he needs sub query, i need 5 different names from a set of top 50 resulted by inner query.
SELECT * FROM t
ORDER BY RAND() LIMIT 5
or from your query result:
SELECT * FROM ( SELECT * FROM t WHERE x=y ) tt
ORDER BY RAND() LIMIT 5
This will give you the number to use as 'm' (limit)
TRUNCATE((RAND()*50),0);
...substitute 50 with n.
To check it try the following:
SELECT TRUNCATE((RAND()*50),0);
I should warn that this could return 0 as a result, is this ok for you?
For example you could do something like this:
SELECT COUNT(*) FROM YOUR_TABLE
...and store the result in a variable named totalRows for example. Then you could do:
SELECT * FROM YOUR_TABLE LIMIT TRUNCATE((RAND()*?),0);
where you substitute the '?' with the totalRows variable, according to the tech stack you are using.
Is it clearer now? If not please add more information to your question.
If I specify a number, say 5, what query will give me all the rows after the 5th row? Like,
SELECT * FROM table WHERE 1=1;
only I want it to exclude the top 5. Thanks.
Use the limit with a very high number as the second argument.
select * from myTable limit 5,18446744073709551615;
From MySQL Docs:
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
BTW: You don't need WHERE 1=1. It doesn't add any value to the query, just clutter.
SELECT * FROM table ORDER BY somecolumn LIMIT 5,1000
http://dev.mysql.com/doc/refman/5.1/en/select.html
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
Are you looking for LIMIT?
SELECT * FROM table LIMIT 5,9999999
The second parameter to limit is just a large number to get all rows. Adjust accordingly.
See: http://dev.mysql.com/doc/refman/5.5/en/select.html
If you have a column whose values can be ordered (and are unique), say ID1, You can do it in pure SQL (e.g. not using the MySQL specific LIMIT) as follows (Syntax is Sybas-ey, may need to tweak table alias and joins to work on mySQL):
SELECT * FROM table WHERE ID1 not in
-- SELECT FIRST #N ROWS IN ACCENDNING ORDER
(SELECT t1.ID1 FROM table 't1', table 't2'
WHERE t1.ID1 < t2.ID2
GROUP BY t1.ID1
HAVING count(*) <= #N)