I want three sets of rows in my query, with 100 in each. Each set with a different data value. So like this:
$result = mysql_query("SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='24' ORDER BY count DESC LIMIT 100") or die(mysql_error());
$result .= mysql_query("SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='23' ORDER BY count DESC LIMIT 100") or die(mysql_error());
$result .= mysql_query("SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='22' ORDER BY count DESC LIMIT 100") or die(mysql_error());
How can I get the result in one query?
Thanks.
use UNION:
SELECT * FROM tableA UNION SELECT * FROM tableB UNION SELECT * FROM tableC
Use union
$final_query=$query1." union ".$query2." union ".$query3;
and
mysql_query($final_query);
Try Union Like
SELECT * FROM tableA UNION SELECT * FROM tableB UNION SELECT * FROM tableC
If you want to allow duplicate as well then try UNION ALL like
SELECT * FROM tableA UNION ALL SELECT * FROM tableB UNION ALL SELECT * FROM tableC
you can use UNION to do this work
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='24' ORDER BY count DESC LIMIT 100)
UNION
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='23' ORDER BY count DESC LIMIT 100)
UNION
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='22' ORDER BY count DESC LIMIT 100)
One way is to use UNION.
$result = mysql_query("
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='24' ORDER BY count DESC LIMIT 100)
UNION
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='23' ORDER BY count DESC LIMIT 100)
UNION
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='22' ORDER BY count DESC LIMIT 100)
");
More options here.
Related
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)
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
I have a table with questions, i want to select random from each category 25 questions.
The table "questions" looks like that:
The categories are: C PC P PP
id question category answer1 answer2 answer3
The easiest way is to use union all with a subquery for each category:
(select * from questions where category = 'C' order by rand() limit 25)
union all
(select * from questions where category = 'PC' order by rand() limit 25)
union all
(select * from questions where category = 'P' order by rand() limit 25)
union all
(select * from questions where category = 'PP' order by rand() limit 25)
If you have a lot of categories or a whole lot of questions (hundreds of thousands or more), then you might want a query that performs better. But for lesser amounts of data, this is probably fine.
I want to emphasize that union all is better for such a query than union. union removes duplicates adding addition processing that should not be needed in this case.
Not sure what you mean by i want to select random from each category but you can just fetch 25 question from each category and union them like
select question from sometable where category = 'C' limit 25
union
select question from sometable where category = 'PC' limit 25
union
select question from sometable where category = 'P' limit 25
union
select question from sometable where category = 'PP' limit 25
Used an outer query with ORDER BY RAND() to select records which are selected from the inner query in a random fashion.
SELECT * FROM
(
(SELECT * FROM questions WHERE category='C' ORDER BY RAND() LIMIT 25) T1
UNION
(SELECT * FROM questions WHERE category='PC' ORDER BY RAND() LIMIT 25) T2
UNION
(SELECT * FROM questions WHERE category='P' ORDER BY RAND() LIMIT 25) T3
UNION
(SELECT * FROM questions WHERE category='PP' ORDER BY RAND() LIMIT 25) T4
) TT
ORDER BY RAND()
This is a HINT you can work upon.
SYNTAX:
Use this query :
SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` )
In place of 'table' put 'questions'.(This is because you have a table name 'questions').
Hope this will help.
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;
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";