I have a MySQL table called campaigns like the following
ID integer
Name text
isBalanced boolean
redirects tinyint
I am trying to achieve a SELECT query so that I get ALL the rows where isBalanced
is false PLUS only a DEFINED number of rows where isBalanced
is true. After I get this result I need to get the single one with the lowest number of redirects
.
I know that there are ways to do it but I am having an hard time to figure out how. This is what I got until know
SELECT campaigns.id, campaigns.name, campaigns.isBalanced, campaigns.redirects
FROM campaigns AS c
INNER JOIN campaigns_2 AS c2
WHERE c.isBalanced = 0
However I am not able to select just some of them from the c2 set and then regroup all the filter to get the single one with lowest number of redirects
.
Any help would be greatly appreciated.
It sounds like you want union all
, perhaps with order by
and limit
:
select c.*
from campaigns c
where c.isBalanced = 0
union all
(select c.*
from campaigns c
where c.isBalanced = 1
limit ??
)
order by redirects
limit 1;
The "defined number" goes where the ??
is.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.