combine two results into one result set mysql - mysql

I have two queries one will return data ordered by likes and in the user city the other one return data by the distance .
so if query 1 return : id 1,2,3 (order by likes)
and query 2 return : id 4,5,6 (order by distance)
i need the final set results to be 1,2,3,4,5,6
i've tried to do union between the two queries but it's not working. any other suggestions ?

You can use left join or union according to this link.
Union ALL also works like you can see here.
Example: SELECT 1 UNION ALL SELECT 2

the solution was to put a limit to each query then the union will work correct :
(SELECT DISTINCT ID, 'a' as type,... FROM table1 GROUP BY ID ORDER BY likesDESC limit 50) union all( SELECT DISTINCT ID, 'b' as type,....FROM table1 GROUP BY ID ORDER BY distance limit 50) order by type asc.

Related

How do I select some rows and then others in MySQL?

I have this query, it works but I'm not sure if it's the best approach and I don't get what I want.
I need to select the query contained in the "IN" clause first, then union with others. Entire row returned must be 40.
SELECT *
FROM (
SELECT * FROM tbl_x a WHERE id IN(11,20,30)
UNION ALL
SELECT * FROM tbl_x b WHERE exam_group='jpx' AND subject='chemistry'
) ab
GROUP BY id LIMIT 40
The next query should to return same data in simple way:
SELECT *
FROM tbl_x
WHERE
id IN (11,20,30)
OR (exam_group='jpx' AND subject='chemistry')
ORDER BY id IN (11,20,30) DESC, id
LIMIT 40;

How to find the top three column totals using SQL

Trying to sum all columns in my table and to find the top 3 of them.
columns have only a value of 1 or 0. That's why I am trying to sum the all inputs to compare them with each other.
I've stucked with order by code integrated into sum()
SELECT (ID)
FROM Student
ORDER BY SUM(C1), SUMC(C2)...SUM(C10)
lIMIT 3
If I understand correctly, you can use union all to calculate the sum for each column and then order by and limit:
select c.*
from ((select 'col1', sum(col1) as s from t) union all
(select 'col2', sum(col2) as s from t) union all
. . .
(select 'col10', sum(col10) as s from t)
) c
order by s desc
limit 3;
try -
SELECT TOP 3 (ID)
FROM Student
GROUP BY (ID)
ORDER BY SUM(C1), SUMC(C2)...SUM(C10) DESC

Query for selecting duplicates + finding lowest value

I have 3 tables in 3 different databaes; Currently the goal here is to find the duplicates unique ID in the three databases and then find the lowest price value of the duplicates unique ID.
Currently I'm using a INNER JOIN to query between only 2 database... Can anyone advise on how to add the third one?
set #a = (SELECT db1.tb1.var1 from db1.tb1
INNER JOIN db2.tb1 ON db2.tb1.var1 = db1.tb1.var1
UNION );
Also, once I have the #a variable set to the duplicate, I wanted to grab a secondary value here.
SELECT price
FROM db1.tb1
WHERE asin=#a
UNION ALL
SELECT price
FROM db2.tb1
WHERE asin=#a
UNION ALL
SELECT price
FROM db3.tb1
WHERE asin=#a
However, the result I'd get would return multiple rows (obviously), How do I query only for the MIN() number from this ?
Any help is appreciated.
Thanks,
Put your query into a subquery, and then use MIN() in the main query.
SELECT MIN(price)
FROM (
SELECT price
FROM db1.tb1
WHERE asin=#a
UNION ALL
SELECT price
FROM db2.tb1
WHERE asin=#a
UNION ALL
SELECT price
FROM db3.tb1
WHERE asin=#a) AS x
You can use order by and limit:
SELECT price
FROM db1.tb1
WHERE asin = #a
UNION ALL
SELECT price
FROM db2.tb1
WHERE asin = #a
UNION ALL
SELECT price
FROM db3.tb1
WHERE asin = #a
ORDER BY price
LIMIT 1;

SELECT from external data in MySQL

Let's consider a made up example
SELECT id, name, score.score FROM
someTable,
(select someTableId, count(*) as score FROM SecondTable GROUP BY someTableId) as score
WHERE score.someTableId == id
ORDER BY score.score DESC
Let's now assume that I have a backend computing my scoring, and that I would like to remove the subquery and insert my own data instead. I would like to know how to do this.
I would like to do something like (this is the question, because what's below doesn't work):
SELECT id, name, score.score FROM
someTable,
((12,324), (1, 342)) as score(id, score)
WHERE score.someTableId == id
ORDER BY score.score DESC
Here is an example of external data substitution to a subquery:
SELECT * FROM users WHERE id IN (SELECT user_id FROM posts WHERE thread_id = 12 GROUP BY user_id);
Without a subquery and with external data:
SELECT * FROM users WHERE id IN (1,2,3);
If I understood you correctly :
SELECT id, name, score.score FROM
someTable,
(SELECT 12 as someTableId,324 as score UNION ALL SELECT 1, 342 <UNION ALL....>) as score(id, score)
WHERE score.someTableId == id
ORDER BY score.score DESC
Thats the only way you can do it, it doesn't actually replace the the subquery, but it replace the select from the table and can improve performance if thats what you are looking for.
In MySQL you don't need to specify a from clause like a dummy table when you are just looking to fetch dummy data.
Other DBMS require a dummy table name (typically DUAL) but in MySQL it's rather straightforward:
SELECT 12 AS id, 324 AS score
UNION ALL SELECT 2, 65
UNION ALL SELECT 3, 598
UNION ALL SELECT 4, 244
You can use this as any other result-set.

select max() from result of count for distinct ids in mysql

I have a table with the following structure:
id name
1 X
1 X
1 Y
2 A
2 A
2 B
Basically what I am trying to do is to write a query that returns X for 1 because X has repeated more than Y (2 times) and returns A for 2. So if a value occurs more than the other one my query should return that. Sorry if the title is confusing but I could not find a better explanation. This is what I have tried so far:
SELECT MAX(counted) FROM(
SELECT COUNT(B) AS counted
FROM table
GROUP BY A
) AS counts;
The problem is that my query should return the actual value other than the count of it.
Thanks
This should work:
SELECT count(B) as occurrence, A, B
FROM table
GROUP BY B
ORDER BY occurrence DESC
LIMIT 1;
Please check: http://sqlfiddle.com/#!9/dfa09/3
You can try like this using a GROUP BY clause. See a Demo Here
select *, max(occurence) as Maximum_Occurence from
(
select B, count(B) as occurence
from table1
group by B
) xxx
This is how I finally handled my problem. Not the most efficient way but get the job done:
select A,B from
(select A,B, max(cnt) from
(select A ,B ,count(B) as cnt
from myTable
group by A,B
order by cnt desc
) as x group by A
) as xx