I have a table with the following columns:
playlistid
trackname
playstatus
likes
dislikes
created
playstatus can have the following values:0, 1, 2 and 4.
The sql query that Im using now is:
SELECT * from playlist
WHERE playlistid=$myplaylistid && playstatus=0
ORDER BY (likes-dislikes) DESC, created ASC
Its worth to mention that there at all times only will be one row with playstatus = 1, one row with playlistid=3, but there will be multiple rows with playstatus = 0 and 2.
I want to change the query above so that I get 1 row from where playstatus=3, 1 row from where playstatus=1, and 3 rows from where playstatus=0 in one single query. All these have to be chosen based on the same orderby that you see in the query above.
All help is appreciated! Thank you for your time!
Try this query
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=1
ORDER BY (likes-dislikes) DESC, created ASC limit 1
union
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=3
ORDER BY (likes-dislikes) DESC, created ASC limit 1
union
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=0
ORDER BY (likes-dislikes) DESC, created ASC limit 3
Try this
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=3
union
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=1
union
SELECT * from playlist WHERE playlistid=$myplaylistid && playstatus=0
ORDER BY (likes-dislikes) DESC, created ASC
Related
I want to select last 5 records from first 50 records in the table, currently i have following query, somebody tell me best way to select these records without calculating the limit and offset?
SELECT id FROM table WHERE enabled=1 ORDER BY date LIMIT 5, 45
Try this
SELECT id FROM (SELECT id FROM (SELECT id FROM table ORDER BY id ASC LIMIT 50) AS tbl ORDER BY id DESC LIMIT 5) as tbldata ORDER BY id ASC
this works:
SELECT id FROM (SELECT id,date FROM table ORDER BY date LIMIT 50) AS
temptable ORDER BY date DESC LIMIT 5
Is possible, and how to ask MySQL for
SELECT * FROM my_table ORDER by row_id DESC LIMIT 8
get the last 8, newest record from my table, with randomized order for PHP showing method
$results = $mysqli->query($query);
while($row = $results->fetch_assoc()) {
echo $row['my_col_name'];
}
Colud I, and where put the rand() in my SQL query?
Without randomize I get last 8 rows ORDERED 10,9,8,7,6,5,4,3
I want to get in the following order:
9,7,5,4,6,10,3,8;
8,7,3,6,10,9,5,4
...
You can place it inside another select:
SELECT * FROM (SELECT * FROM my_table ORDER by row_id DESC LIMIT 8) t ORDER BY RAND()
Use a subquery:
SELECT t.*
FROM (SELECT t.*
FROM my_table t
ORDER by row_id DESC
LIMIT 8
) t
ORDER BY rand();
i want to get the last 6 rows but skip the last one in result:
SELECT * FROM stats_follow ORDER BY id DESC LIMIT 6
How can i make this?
Try this:
SELECT *
FROM (SELECT *
FROM TableName
ORDER BY FieldName DESC
LIMIT 5) sub
ORDER BY FieldName ASC
Try this:
SELECT * FROM stats_follow ORDER BY id DESC LIMIT 1,6
I am trying to create a mysql query to select let's say 5 rows before and 5 rows depending on the currend ID. Here is the query I am trying to use:
SELECT * FROM posts WHERE PID<='10' ORDER BY PID DESC LIMIT 7
UNION ALL
SELECT * FROM posts WHERE PID>'10' ORDER BY PID ASC LIMIT 6
For some reason though, the query is not working. Am I missing something? I found this solution in a forum post.
Try this:
(SELECT *
FROM posts
WHERE PID <= '10'
ORDER BY PID DESC LIMIT 7
)
UNION ALL
(SELECT *
FROM posts
WHERE PID > '10'
ORDER BY PID ASC LIMIT 6
)
ORDER BY PID ASC;
sqlfiddle demo
The PID is likely of int type. So you should be using the following (SQL Fiddle):
(SELECT * FROM posts WHERE PID <= 10 ORDER BY PID DESC LIMIT 7)
UNION ALL
(SELECT * FROM posts WHERE PID > 10 ORDER BY PID ASC LIMIT 6)
As you can see the ordering of the two queries is also maintained separately.
DOCS
To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:
I have a feature that gets the record for specific id and has two links for the previous and next record. Right now I have separate queries to obtain the next id:
$query = "SELECT id FROM presentations WHERE id > '$getId' ORDER BY id ASC LIMIT 1";
$getId is the current id.
Is there a way to consolidate into one query?
SELECT A.id AS previd, B.id AS nextid
FROM
(SELECT id FROM presentations WHERE id < '$getId' ORDER BY id DESC LIMIT 1) A,
(SELECT id FROM presentations WHERE id > '$getId' ORDER BY id ASC LIMIT 1) B
Returns 2 columns: previd and nextid surrounding $getId
i think you can use inner query
SELECT id FROM presentations
WHERE id >= ( SELECT id FROM presentations WHERE id < '$getId' ORDER BY id DESC LIMIT 0 , 1 )
ORDER BY id ASC LIMIT 0 , 3
first array result will be previous record, second will be your current record, and the last will be your last record.