I want to combine these two queries.
SELECT * FROM table1 WHERE status='pending' and adr='' order by id desc limit 0,1;
SELECT * FROM table1 where status='pending' and adr='new' ORDER BY RAND() LIMIT 1
You can use a UNION ALL set operator to concatenate the results of the two queries
( SELECT * FROM table1 WHERE status='pending' AND adr='' ORDER BY id DESC LIMIT 1 )
UNION ALL
( SELECT * FROM table1 WHERE status='pending' AND adr='new' ORDER BY RAND() LIMIT 1 )
Reference: UNION ALL https://dev.mysql.com/doc/refman/5.5/en/union.html
Related
I'm attempting a mysql union but nothing is returned? Below is an example. In total there is 16 select queries
$url_array = array(
"euro-gbp","euro-aud","euro-usd",
"euro-jpy","gbp-jpy","euro-cad",
"usd-cad","usd-jpy","cad-jpy",
"gbp-usd","aud-usd","gbp-cad",
"aud-cad","aud-jpy","aud-nzd",
"euro-nzd","gbp-aud","gbp-nzd",
"nzd-usd","nzd-cad","nzd-jpy");
foreach($url_array as $urls) {
$sql[]= "SELECT *
FROM `data_analysis_child`
WHERE type='".$urls."'
ORDER BY id DESC
LIMIT 2";
}
$sql = implode(" UNION ",$sql);
The sql result
SELECT * FROM `data_analysis_child` WHERE type='euro-gbp' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='euro-aud' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='euro-usd' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='euro-jpy' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='gbp-jpy' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='euro-cad' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='usd-cad' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='usd-jpy' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='cad-jpy' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='gbp-usd' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='aud-usd' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='gbp-cad' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='aud-cad' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='aud-jpy' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='aud-nzd' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='euro-nzd' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='gbp-aud' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='gbp-nzd' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='nzd-usd' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='nzd-cad' ORDER BY id DESC LIMIT 2 UNION SELECT * FROM `data_analysis_child` WHERE type='nzd-jpy' ORDER BY id DESC LIMIT 2
How do i solve? Is there a better way to restructure this query?
if you need order by and limit for each select you should use () around each select
foreach($url_array as $urls) {
$sql[]= " ( SELECT * FROM `data_analysis_child` WHERE type='".$urls."' ORDER BY id DESC LIMIT 2 )";
break;
}
Just a little addition to do that. If you are getting ZERO results then use UNION ALL instead of only UNION. Actually duplicate records are avoided in UNION but with UNION ALL you will get everything that a query is returning. give it a try !!!
The documentation of UNION clearly explains:
To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:
(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
It also says:
Note
Previous versions of MySQL may permit such statements without parentheses. In MySQL 5.7, the requirement for parentheses is enforced.
If you are using MySQL 5.7 or newer, the code doesn't return anything because the query doesn't run. You should check the type of the returned value (the query functions return FALSE when the query is invalid) and the error message.
You could do this:
SELECT *
FROM (
SELECT #rank := if (#last_type=type, #rank+1, 1) as type_rank, *
FROM data_analysis_child
WHERE type IN ('euro-gbp', 'euro-aud', 'euro-usd'....)
ORDER BY id DESC
)
WHERE type_rank<=2;
But the performance will suck.
Depending on how the indexes are configured and assuming that your id column is unique this might be better:
SELECT 'first', dac1.*
FROM data_analysis_child dac1
WHERE dac1.id IN (
SELECT MAX(dac2.id)
FROM data_analysis_child dac2
WHERE dac2.type IN ('euro-gbp', 'euro-aud', 'euro-usd'....)
GROUP BY dac2.type
)
UNION
SELECT 'second', dac3.*
FROM data_analysis_child dac3
WHERE dac3.id IN (
SELECT MAX(dac4.id)
FROM data_analysis_child dac4
WHERE dac4.type IN ('euro-gbp', 'euro-aud', 'euro-usd'....)
AND dac4.id NOT IN (
SELECT MAX(dac5.id)
FROM data_analysis_child dac5
WHERE dac5.type IN ('euro-gbp', 'euro-aud', 'euro-usd'....)
GROUP BY dac5.type
)
GROUP BY dac4.type
)
But it does not scale well to getting the top N values for each type.
A better solution would be to to flag the state of the first or second rank as an attribute on the record and filter directly.
I would like to get values without the smallest and the biggest ones, so without entry with 2 and 29 in column NumberOfRepeating.
My query is:
SELECT Note, COUNT(*) as 'NumberOfRepeating'
WHERE COUNT(*) <> MAX(COUNT(*))AND COUNT(*) <> MIN(COUNT(*))
FROM Note GROUP BY Note;
SELECT Note, COUNT(*) as 'NumberOfRepeating'
FROM Notes
GROUP BY Note
HAVING count(*) <
(
SELECT max(t.maxi)
FROM (select
Note, COUNT(Note) maxi FROM Notes
GROUP BY Note
) as t
)
AND
count(*) >
(
SELECT min(t.min)
FROM (select
Note, COUNT(Note) min FROM Notes
GROUP BY Note
) as t
)
try this code.
One method would use order by and limit, twice:
select t.*
from (select t.*
from t
order by NumberOfRepeating asc
limit 99999999 offset 1
) t
order by NumberOfRepeating desc
limit 99999999 offset 1;
Try this code,
Select * from Note where NumberOfRepeating < (select MAX(NumberOfRepeating) from Note ) AND NumberOfRepeating > (select MIN(NumberOfRepeating) from Note );
Here in the code, as in your table Note is the name of the table, and NumberOfRepeating is the column name, as in your table.
Try this. It should work
SELECT *
FROM ( SELECT Note, COUNT(*) as 'NumberOfRepeating'
FROM Notes
GROUP BY Note
ORDER BY NumberOfRepeating DESC
LIMIT 1, 2147483647
) T1
ORDER BY T1.NumberOfRepeating
LIMIT 1, 2147483647
I want to select the last 3 rows of an sql table. I know I should use SELECT * FROM table ORDER BY DESC LIMIT 3, but the problem with this code is that it selects the rows from the end. For example, it selects 30, then 29, then 28. But, I need them in this format: 28, 29, 30. Any suggestion?
Try this:
SELECT * FROM (
SELECT * FROM reset ORDER BY id DESC LIMIT 3
) as r ORDER BY id
I hope this help your problem
select * from
(
select * from reset
order by id DESC LIMIT 3
) t
order by id ASC
Try something like this:-
SELECT * FROM reset
WHERE username = '$table' ORDER BY id ASC LIMIT (FOUND_ROWS() - 3), 3
How about something like:
select * from (select * from table order by x desc limit 3) order by x;
try
Select * from (SELECT * FROM Table_name ORDER BY Column_name DESC limit 0,3) as alias ORDER BY Column_name ASC;
try this manual one !
easy and Simple !!
Select * From tableName where
PKCol=(select count(*) from tableName )
OR
PKCol=(select count(*) from tableName )-1
OR
PKCol=(select count(*) from tableName )-2
order by PKCol desc;
It will help you out to give latest 3 rows data, if you wanna take first 3 rows then ASC instead of DESC.
select distinct column_name from Table order by column_name desc limit 3;
I'm using an union statement in mysql but i've some problems sorting the results. The ORDER statement doesn't works at all, the results comes out always sorted by the id field.
Here an example query:
SELECT a.* FROM ( ( select * from ticket_ticket AS t1 WHERE ticket_active=1 ORDER BY t1.ticket_date_last_modified DESC )
UNION ( select * from ticket_ticket AS t2 WHERE ticket_active=0 ORDER BY t2.ticket_date_last_modified DESC, t2.ticket_status_id DESC ) )
AS a LIMIT 0,20;
I want to order the results of the first SELECT by last_modified time, and the second SELECT by time and status. But the ORDER statement get just skipped. The results always come out ordered by the ticket_id ( the PRIMARY KEY ).
What's wrong in this query ?
Thanks!
Ok, i've fixed it writing the query this way:
SELECT a.*
FROM
(SELECT *
FROM ticket_ticket
WHERE ticket_active=1
ORDER BY ticket_date_last_modified DESC) AS a
UNION ALL
SELECT b.*
FROM
(SELECT *
FROM ticket_ticket
WHERE ticket_active=0
ORDER BY ticket_date_last_modified DESC, ticket_status_id DESC) AS b LIMIT 0,
20;
You are using a UNION query that will return distinct values, and the order of the returned rows is not guaranteed.
But you don't need an union query for this:
select *
from ticket_ticket AS t1
ORDER BY
ticket_active!=1,
ticket_date_last_modified DESC,
ticket_status_id DESC
LIMIT 0,20;
SELECT DISTINCT ID FROM (SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4 ORDER BY total_hits DESC) AS sumtbl LIMIT 50;
This query works fine and selects unique ID's ordered by total_hits DESC, the question is how can I return total_hits column too having Id's unique?
SELECT ID, SUM(total_hits)
FROM (
SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4
) AS sumtbl
GROUP BY ID
ORDER BY SUM(total_hits) DESC
LIMIT 50;
If you want the total_hits FROM all the ids across all the tables, you'll need to do a sum / group by. Not sure if this is what you're asking since the question is vague...
SELECT DISTINCT id, sum(total_hits) total_hits
FROM (SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4) AS sumtbl
GROUP BY id
ORDER BY total_hits DESC
LIMIT 50
Also, don't use select * as it's bad practice, esp. if you go to add a column to one of those tables and not the others, your whole query will break.
UPDATE
In your case you can do
SELECT ID, MAX(total_hits) as max_hits
FROM (
SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4
)sumtbl
GROUP BY ID
ORDER BY max_hits DESC
LIMIT 50
Note : you don't need ORDER BY in derived query, it goes to upper
Also, it would be better, if you
SELECT id, total_hits
FROM tbl1
UNION ALL
SELECT id, total_hits FROM tbl2 etc. rather then selecting all fields from tables in your derived query.