I want to display two records.
For eg
select * FROM users WHERE user_id = 5.
Now i want another row randomly selected from users table but with user_id != 5
Is it possible to do in a single query. I tried using union all but i dnt get two distinct rows.
Thanks
This works fine for me. The first result is always the record with ID 5, the second row is a random one. Note that if no record with the ID 5 exists, both rows will be random.
SELECT * FROM users ORDER BY (user_id = 5) DESC, RAND() LIMIT 0,2
Try this
SELECT * FROM users WHERE user_id = 5 || user_id != 5 ORDER BY RAND() LIMIT 2
Union should do the trick. You probably forgot LIMIT.
(SELECT * FROM users WHERE user_id = 5 LIMIT 1)
UNION
(SELECT * FROM users WHERE user_id != 5 LIMIT 1)
Try this:
SELECT * FROM users WHERE user_id=5
union all (
SELECT * FROM users WHERE user_id!=5
ORDER BY RAND() LIMIT 0,1)
Related
function rand(); work slow when have millions rows,
I have table 'banners'
id image type
1 name 1
2 name 4
3 name 76
19999999 name 3
need to select
select * from banners where type = 1 order by rand() limit 1
if no result then
select * from banners where type = 3 order by rand() limit 1
if no result then
select * from banners order by rand() limit 1
I try
select * from (
(select * from banners where type = 1 order by rand() limit 1) union
(select * from banners where type = 3 order by rand() limit 1) union
(select * from banners order by rand() limit 1)
) as r limit 1
but is very slow!
You can combine your queries:
select *
from banners
order by type=1 desc, type=3 desc, rand()
limit 1;
but that's still likely to have to read the entire table.
Do you have INDEX(type)? If so, that might help your first 2 SELECTs.
Provide SHOW INDEXES. It may indicate that the "cardinality" of type is such that it will 'always' or 'never' do a table scan. Also, provide EXPLAIN SELECT ... for each case.
If the index is useful and there are not many rows with type=1, that query will be relatively fast. Etc.
This discusses several ways to efficiently pick a random row. See which one applies for each of your 3 cases. Then write a Stored Routine that works something like
BEGIN
... type=1
IF a row found
RETURN...
... type=1
IF a row found
RETURN...
Return a random row from the whole table
END
select * from banners
order by Case
when Type=1 then 1
when Type=3 then 2
else 3
end asc, rand()
limit 1;
I have issue there with select from table. I want to select all rows except for first row. So .. There is my code
SELECT * FROM table ORDER BY id DESC
So this code select and order id's from table which give me id feedback "5>4>3>2>1". And there is issue .. How I can select and echo just 4>3>2>1 rows.
So if I had rows with id's 1,2,6,8,10 , echo will be 10,8,6,2,1 and I want select to echo just 8,6,2,1.
There is my full wrong code for select.
$other = mysql_query("SELECT * FROM table ORDER BY id DESC LIMIT 1, 1");
This should do it.
SELECT * FROM table WHERE id NOT IN (SELECT MAX(id) FROM table) ORDER BY id DESC
Try this:
SELECT *
FROM
(
SELECT *, row_number()
OVER (ORDER BY id DESC) row
FROM table
)
WHERE row != 1
It gives numbers to your selected rows and takes all of them without the one with row number 1
Try this
$other = mysql_query("SELECT * FROM table ORDER BY id DESC OFFSET 1");
THE ABOVE QUERY WONT WORK AS A LIMIT IS NEEDED
Refer to this answer
All you need is offset 1, but offset cannot be used without limit. So, I'd suggest something like:
SELECT * FROM table ORDER BY id DESC LIMIT 99999999 OFFSET 1
Warning: make sure your table doesn't contain lots of records, otherwise you will run into performance issues. Or, change the limit to something reasonable, like 10.
EDIT:
Read: How to use offset without limit
SELECT *
FROM table
WHERE id NOT IN ( SELECT id
FROM table
ORDER BY id DESC
LIMIT 1 )
ORDER BY id DESC;
You can try this.
In this case I am selecting all the rows, except the one with the biggest id.
In the example of [1,2,3,4,5], it will be :
SELECT *
FROM table
WHERE id NOT IN ( 5 )
ORDER BY id DESC;
Hope this helps!
I want to run a query where the last 5 entries are always returned, which is simple: SELECT * FROM table ORDER BY id DESC LIMIT 5. But I want to get more than 5 if a certain condition applies, which by itself would simply be something like SELECT * FROM table WHERE field > value. Is there a way to do this as a single query or do I have to run 2?
Maybe with UNION?. Like:
(SELECT * FROM table ORDER BY id DESC LIMIT 5)
UNION
(SELECT * FROM table WHERE field > value)
I am trying fetch each user's info and user's number from PHP database. So, the problem is that sometimes user adds more than 1 number. HERE is tables and SQL:
users table
id user_name email
1 John john#gmail.com
2 David david#gmail.com
numbers table
id user_id number number_prefix
1 1 3144425 123
2 1 5484248 123
3 1 3144425 325
4 2 5991599 123
As you see, 1st user's have 3 number.
Here is my sql:
SELECT
u.*,
n.*
FROM
users u,
numbers n
WHERE
u.id = n.user_id AND
u.id = 1
OF course it will work. I want to set different LIMIT for each tables. I mean to set LIMIT 1 for table: users, and set LIMIT 3 for table: numbers
To do it, I used another sql, like below:
SELECT *,
(
SELECT
number
FROM
menu
WHERE
user_id = 1
ORDER BY id
LIMIT 0,3) AS number
FROM users
WHERE id = 1
ORDER BY id
LIMIT 0,1
As you know sub query can not return more than 1 row Maybe, IT should be better TO use SQL JOIN and GROUP BY... I have tried, but was not any good result...
Use the subqueries in a join, then it can return multiple rows.
SELECT *
FROM (SELECT number
FROM menu
WHERE user_id = 1
ORDER BY id
LIMIT 3) AS number,
JOIN (SELECT *
FROM users
WHERE id = 1
LIMIT 1) AS users
SELECT *
FROM (SELECT * FROM users LIMIT 1) AS us , numbers ns
WHERE us.id = '1' LIMIT 3;
Lets say I have a 1000 rows in my table.
I want to select 10 of those at random.
SELECT * FROM table ORDER BY RAND() LIMIT 10
Then I want to select the row in that result with the highest value for number
SELECT * FROM table ORDER BY number DESC LIMIT 1
Can anyone help me come up with an efficient way of doing this?
Just use a subquery:
SELECT *
FROM (
SELECT * FROM table ORDER BY RAND() LIMIT 10
)
ORDER BY number DESC LIMIT 1