How to get limit records with in Clause - php

I have a following query
I am getting id as a string
SELECT * from table_name
where id in (1,2,3,4)
order by created desc
limit 0,4
this Query is giving me only 4 records
i want to get 4 records from each id
Any idea ?

select * from
(
SELECT * from table_name where id = 1 order by created desc limit 4
union all
SELECT * from table_name where id = 2 order by created desc limit 4
union all
SELECT * from table_name where id = 3 order by created desc limit 4
union all
SELECT * from table_name where id = 4 order by created desc limit 4
) tmp
order by created desc

Related

Writing a query that returns the latest row related to a specific user

Trying to create a query that gets the most recent record related to the user. Here's my attempt
"SELECT *
FROM (
SELECT *
FROM mytable
ORDER BY id
DESC LIMIT 1)
WHERE userid = $userID";
You could also:
Select * from table
Where id =
(Select Max(Id) from table
where userid = $userID)
Simply skip the sub-query:
SELECT *
FROM mytable
WHERE userid = $userID
ORDER BY id DESC
LIMIT 1
Use Top(1)
Select TOP(1) * FROM table ORDER BY id DESC
Try:
SELECT *
FROM mytable
WHERE userid = $userID
ORDER BY id DESC
LIMIT 1

MySQL select grouping multiple rows from same table

I'm trying to create a query to select multiple rows from the same table grouping them like an array.
Now i'm selecting them using php like this:
$tks = mysqli_query($con,"SELECT * FROM hof ORDER BY tks DESC LIMIT 5");
$top_ths = mysqli_query($con,"SELECT * FROM hof ORDER BY ths DESC LIMIT 1");
$top_tha = mysqli_query($con,"SELECT * FROM hof ORDER BY tha DESC LIMIT 1");
----
I would like to merge them in a single query so i get an associative array.
Something like this:
(SELECT * FROM hol ORDER BY tks DESC LIMIT 5) AS tks
UNION
(SELECT * FROM hol ORDER BY ths DESC LIMIT 1) AS top_ths
So tks contains all the 5 rows and top_ths contains 1 row.
Is it possible ? Thanks.
to undersatnd from wich group row is, make additional field
(SELECT *, 1 as `group` FROM hol ORDER BY tks DESC LIMIT 5)
UNION
(SELECT *, 2 as `group` FROM hol ORDER BY ths DESC LIMIT 1)

MySQL Rand() not in subquery?

I have a set of tracks that need to be played,
There are something like 70 tracks in the database, and my script need to generate a new ID to play in order to start the next track.
Current query: ($row['v_artist'] is the current artist playing)
SELECT *
FROM t_tracks
WHERE v_artist NOT LIKE '%".$row['v_artist']."%'
ORDER BY RAND()
LIMIT 1;
Now I wish to add a subquery to rand() so that it picks a random id, but not from the first 50 (NOT IN?)
Subquery:
SELECT *
FROM `t_playlist`
ORDER BY pl_last_played DESC
LIMIT 50, 1
How can I get a random ID from t_tracks that does not exist in the query for t_playlist?
Conceptually, I think you want this:
SELECT *
FROM t_tracks
WHERE v_artist NOT LIKE '%".$row['v_artist']."%' AND
track_id NOT IN (SELECT track_id FROM t_playlist ORDER BY pl_last_played DESC LIMIT 50)
ORDER BY RAND()
LIMIT 1;
However, MySQL doesn't permit LIMIT in some subqueries, so use LEFT JOIN instead:
SELECT t.*
FROM t_tracks t LEFT JOIN
(SELECT track_id
FROM t_playlist
ORDER BY pl_last_played DESC
LIMIT 50
) p
ON t.track_id = p.track_id
WHERE t.v_artist NOT LIKE '%".$row['v_artist']."%' AND
p.track_id IS NULL
ORDER BY RAND()
LIMIT 1;

MySQL Query - Show Latest 3 Records with Order by Ascending

Is there anyway to fetch latest 3 comments with Order By id asc ?
Here is my table Structure: Table name: comments
Right Now I am using this Query:
SELECT *
FROM `comments`
ORDER BY id ASC
LIMIT 0 , 3
But it returns in result, which is obvious :
But I want to show latest 3 records , but in Ascending Order.
Like this:
Use below code:
SELECT *
FROM (SELECT *
FROM `comments` ORDER BY id DESC LIMIT 0 , 3) t
ORDER BY id ASC;
First you sort by descending id, and get 3 results, and then do ascending sort on id on these 3 results.
(SELECT * FROM `comments` ORDER BY id DESC limit 3 ) ORDER BY id ASC
Just reorder the DESC query with a second ORDER BY :)
SELECT * FROM (
SELECT *
FROM comments
ORDER BY id DESC
LIMIT 3
) t ORDER by id ASC
try this
select * from (select * from `comments` ORDER BY id desc limit 0,3) t
order by id asc;
This should do it:
SELECT *
FROM `comments`
ORDER BY id DESC
LIMIT 0 , 3

Echo data from several sql tables

I have never tried this but want to echo data from several different tables and order them by their timestamp. The code I have comes up with a booleon and i dont know why.
<?php
$result = mysql_query("SELECT * FROM artists ORDER BY timestamp DESC
UNION
SELECT * FROM news ORDER BY timestamp DESC
UNION
SELECT * FROM tracks ORDER BY timestamp DESC
UNION
SELECT * FROM gigs ORDER BY timestamp DESC
UNION
SELECT * FROM feature ORDER BY timestamp DESC");
while($row = mysql_fetch_array($result)){echo'News: '.$row['name'].$row['title'].$row['trackname'].$row['gigname'].'<br>' ;}
?>
Assuming your tables fall within the constraints of the UNION function..
Try:
SELECT * FROM
(SELECT * FROM artists ORDER BY timestamp DESC
UNION
SELECT * FROM news ORDER BY timestamp DESC
UNION
SELECT * FROM tracks ORDER BY timestamp DESC
UNION
SELECT * FROM gigs ORDER BY timestamp DESC
UNION
SELECT * FROM feature ORDER BY timestamp DESC) as temp
ORDER by timestamp DESC;

Categories