I have a table in mysql, say table1.
I am running this on it:
SELECT FLOOR( MAX(id) * RAND()) FROM `table1`
This works well, but I am now trying to add a condition of "AND tom".
Where tom is a integer field.
For example:
id tom
1 0
2 3
3 2
4 0
5 0
6 3
7 1
8 1
9 3
etc.
So, my question is,
How can I pick a random value from id, which also satisfies tom='0' say?
SELECT id FROM `table1` WHERE tom = 0 ORDER BY RAND() LIMIT 1
This will first get all rows in which tom = 0,then order those results randomly. MySQL will then limit those results to just one, returning the single value you want to retrieve.
I hope I understood correctly:
SELECT id FROM `table1` WHERE tom = 0 order by rand() limit 1
select * from (
select * from table where tom = 0 ) as t order by rand() limit 1
Related
I have this table
**applications**
id user_id company_id shortlisted
1 10 99 0
2 10 100 1
3 10 101 1
4 10 102 0
5 11 99 1
6 12 99 0
6 12 101 0
What I want is to select all users
which have been shortlisted at-least once
which have not been shortlisted at all
For the first case, i have the following query:
SELECT user_id
from applications
where shortlisted=1
Group
By user_id
and this gives me the expected result like below
**applications**
user_id
10
11
But I'm trying the following query for the second case and it returns me an empty set:
Select user_id
from applications as Application
where shortlisted=0
and NOT EXISTS(Select user_id from applications where user_id=Application.user_id and shortlisted=1)
What am i missing?
PS: Please ignore any typos as i typed them manually for this post.
To get both results in a single query simply use aggregation:
select user_id, max(shortlisted) as was_shortlisted
from applications
group By user_id
You can use group by and having for both.
For the first:
select user_id
from applications
group By user_id
having max(shortlisted) = 1;
For the second:
select user_id
from applications
group By user_id
having max(shortlisted) = 0;
In all honesty, your version with the where is more efficient for the first query. This is just to show how closely related the queries are.
You can try following query;
select user_id from table1
group by user_id having MIN(shortlisted) = 1
This will give you to at least have shortlisted = 1 condition and don't have shortlisted = 0 records.
This question already has answers here:
Select last row in MySQL
(11 answers)
MySQL - Select the last inserted row easiest way
(9 answers)
Closed 5 years ago.
I have a table like this:
id transaction_id auto_recurring paid_amount package_customerid
37 0 1 0 4
45 37 1 0 4
51 0 1 0 4
57 51 1 0 4
62 0 1 0 4
67 62 1 0 4
There are 6 records of package_customer_id = 4. Now I want to get the last record of 4. in this case id = 67 is my desired record. I try this SELECT * FROM transactions GROUP BY package_customer_id. But I got first record of package_customer_id = 4. i.e: id = 4 is my fetched result. How can I get id = 67 (my desired record) modifying this sql?
SELECT * FROM transactions WHERE package_customer_id = 4 ORDER BY id DESC LIMIT 1;
That would be my shot at it. Sorry but i haven't tested it, i leave it up to you:)
EDIT:
Dont forget the quotes " ` " on columns name's:)
Check you column name package_customer_id OR package_customerid ?
You may try this .
SELECT temp.* FROM (SELECT * FROM `transactions` WHERE package_customer_id = 4 order by id DESC LIMIT 1 ) AS temp GROUP BY temp.package_customer_id
Don't use group by. Use where:
SELECT t.*
FROM transactions t
WHERE t.id = (SELECT MAX(t2.id)
FROM transactions t2
WHERE t2.package_customer_id = t.package_customer_id
);
You can filter this for whichever package customer ids you like, in the outer query.
You can use like this:
SELECT * FROM transactions WHERE id IN (SELECT MAX(id) FROM transactions GROUP BY package_customerid)
try this query
if you need with GROUP BY clause: use this query
SELECT * FROM transactions where GROUP BY package_customerid ORDER BY package_customerid DESC LIMIT 1;
OR
if you DON'T need with GROUP BY clause: use this query
SELECT MAX(id),transaction_id,auto_recurring,paid_amount,package_customerid FROM transactions where package_customerid=4;
Output:
id transaction_id auto_recurring paid_amount package_customerid
67 62 1 0 4
I have a table:
Numbers
id type_id
1 1
2 1
3 2
4 1
5 2
6 2
7 1
8 1
9 2
etc...
I need to get 3 random records of type 1 and the same number of random records of type 2. How can I get it with one query?
(select * from your_table where type_id = 1 order by rand() limit 3)
union all
(select * from your_table where type_id = 2 order by rand() limit 3)
Using MySQL
SELECT <<columns>> FROM <<table_name>>
ORDER BY RAND()
LIMIT <<count>>
Using Oracle
SELECT * FROM (SELECT * FROM <<table_name>> ORDER BY
SYS.DBMS_RANDOM.VALUE) WHERE ROWNUM <<Count>>
I'm trying to select rows where x=5, but x changes constantly. So I have such a table:
id x
---- ---
1 5
2 6
3 4
4 5
5 5
So I want to perform a query like "SELECT * FROM table WHERE x=5 AND _???_;" so that it returns rows 4 & 5 but not row 1.
In other words, I want to get the rows where x had this value most recently. I hope I made myself clear. Thanks!
edit:
Number of entries after x got the last value my change. I mean the table could also be like this:
id x
---- ---
1 5
2 6
3 4
4 5
5 1
6 5
7 5
... 5
100 5
101 5
in this case it should return rows [6-101].
Following wil get recent row
SELECT * FROM table WHERE x=5 ORDER BY id DESC LIMIT 0,1
SQLFiddle demo
select * from t t1
where
x=(select x from t order by id desc limit 1)
and
not exists
(select x from t where id>t1.id and x<>t1.x)
or
SQLFiddle demo
select * from t t1
where
x=(select x from t order by id desc limit 1)
and
id>=
(select max(id) from t
where x<>
(select x from t order by id desc limit 1)
)
Select what is faster on your base.
I have a query that selects 20 rows from a table, loops, and pulls a single row from the same table that falls in the desired score range. The found row is then deleted so that it will not be selected again.
user_id is unique and some rows have col1=0 and others have col1=1, therefore the second query will never select a row from the first query.
The temp table looks like this:
user_id col1
-------------------
1 0
2 0
3 1
4 1
The user table looks like this:
user_id score
-----------------
1 1000
2 2000
3 3000
4 4000
$res = do_query("SELECT temp.user_id,user.score
FROM temp,user
WHERE temp.col1=0 AND temp.user_id=user.user_id LIMIT 20");
while (($row = mysql_fetch_row($res))) {
$score = $row[1];
$alt_res = do_query("SELECT temp.user_id, user.score
FROM temp,user
WHERE temp.col1=1 AND temp.user_id=user.user_id
AND user.score<$score AND user.score>$score*0.66 LIMIT 1");
$alt_row = mysql_fetch_row($alt_res)
$user_id = $alt_row[0];
do_query("DELETE FROM temp WHERE user_id=$user_id");
}
This works just fine, however I was trying to turn this into a single query, but I keep getting duplicate values, and I can't seem to weed them out.
SELECT temp.user_id,t1.user_id,t1.score FROM (
SELECT temp.user_id,user.score
FROM temp,user
WHERE temp.col1=0 AND temp.user_id=user.user_id LIMIT 20) AS t1,temp,user
WHERE temp.col1=1 AND temp.user_id=user.user_id
AND t1.score<user.score AND t1.score>user.score*0.66 GROUP BY temp.user_id
I get 20 rows with temp.user_id being unique, but duplicates with t1.user_id.
For example:
temp.user_id t1.user_id
----------------------------
1 6
2 7
3 7
4 7
5 8
and I want:
temp.user_id t1.user_id
-----------------------------
1 6
2 7
3 8
4 9
5 10
Any idea how to make it so that no user_id is repeated in either column?
Maybe you can change LIMIT 20 to LIMIT 1 on the sub-query?