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)
Related
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;
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
Inverse PHP mysql_fetch_array
I get last 4 row from database, but I want order these 4 rows in inverse
mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT 4");
How can I do this?
mysql_query("SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 4) t ORDER BY id ASC");
i Think this should do
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
How do I join something like this together:
$query = ("SELECT * FROM profiles,follow
WHERE follow.friend ='$user'
AND follow.user !='$user'
AND profiles.user1 =follow.user
ORDER BY id DESC limit 20");
or
("SELECT * FROM profiles WHERE user1='$user'");
I need the query to display two different things how do I do that.
This one?
SELECT
*
FROM
follows
INNER JOIN
profiles
ON (profiles.user1 = follow.user)
WHERE
follow.friend = '{$user}'
AND follow.user != '{$user}'
AND profiles.user1 = '{$user}'
ORDER BY
profiles.id DESC
LIMIT 20
Use UNION:
SELECT *
FROM profiles, follow
WHERE follow.friend ='$user'
AND follow.user !='$user'
AND profiles.user1 =follow.user
ORDER BY id DESC limit 20
UNION
SELECT *
FROM profiles
WHERE user1='$user'
try this
$query = "SELECT * FROM profiles,follow WHERE follow.friend ='$user' and follow.user !='$user' and profiles.user1 =follow.user and profiles.user1='$user' order by id DESC limit 20";