Sort by number of occurrence in tables - mysql

I have a database with many tables.. and each table has stored only IDs. Now what I want to do is:
SELECT id FROM table1, table2, table3
GROUP BY id;
but I also want to sort them by decreasing order of occurrence.
For example the IDs that are in all 3 tables should appear on top and the IDs appearing in only one table should be at the bottom. Any clue on how to do this?

Try this too
select id from
(
SELECT id FROM table1
union all
select id from table2
union all
select id from table3
) as t
GROUP BY id
order by count(id) desc

select sum(t1.id IS NOT NULL,t2.id IS NOT NULL, t3.id IS NOT NULL) as total,t1.id from table1 as t1 join table2 as t2 on t1.id=t2.id join table3 as t3 on t3.id = t1.id order by total desc;
I am not sure about your problem but this can help

select id from
(
SELECT id FROM table1
union all
select id from table2
union all
select id from table3
) as t
GROUP BY id
order by id desc

Related

Easy query on 2 tables in SQL

I have a table, TABLE1, with two columns: SSN and Date.
I have another table, TABLE2, with SSN, name and Surname.
I want to view the Name and the Surname of the person who is associated to the SSN that appears the highest number of times in the first table (the one with SSN and Date)
Which query should I write?
The fastest way is probably to aggregation before joining:
select *
from (select t1.ssn
from table1 t1
group by t1.ssn
order by count(*) desc
limit 1
) t1 left join
table2 t2
using (ssn)
Try this.
SELECT t1.ssn, t2.name, t2.surname, count(*) as vol
FROM table1 t1
LEFT JOIN table2 t1 on t1.ssn = t2.ssn
GROUP BY 1
ORDER BY count(*) DESC
LIMIT 1
Use a subquery in the WHERE clause which returns the SSN that appears the most in table1:
SELECT *
FROM table2
WHERE SSN = (SELECT SSN FROM table1 GROUP BY SSN ORDER BY COUNT(*) DESC LIMIT 1)
With an index on SSN (which I assume exists) this is the fastest way to get the result.

Why does group by and sort by give 0 response

The result of this query gives me 0 rows where it should give me the 3 latest rows (grouped by Table1.Name).
Table1 has: "Name", "Timestamp", "Voltage".
Table2 has: "Name", "data".
When I delete "ORDER BY Table1.Timestamp" I do get 3 rows (as expected) but they are the 3 oldest entries in the database where I want the 3 latest.
(I have 3 Name values in Table1 and Table2 that match).
The code:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;
You can try to perform a query like this :
SELECT t.*,t2.* from Table1 t
INNER JOIN Table2 t2
ON t.Name=t2.Name
WHERE t.Timestamp = (
SELECT MAX(t3.Timestamp) FROM Table1 t3
WHERE t3.Name = t.Name
)
You could sort and taking the top 3:
SELECT * from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
ORDER BY Table1.Timestamp DESC
LIMIT 3
When you are using join two or more tables then don't use * in select query. Use specific column name to avoid column ambiguously in select query , you can also use Table1., Table2. but good way to use specific columns in select query.
SELECT Table1.Name,Table1.Timestamp from Table1
INNER JOIN Table2
ON Table1.Name=Table2.Name
GROUP BY Table1.Name
ORDER BY Table1.Timestamp;

Select most recent rows from inner joined tables in MySQL

I want to get the most recent row from each inner joined tables. Both tables have a timestamp field. Below is what I have so far. But it only targets for table1 how about for table2?
SELECT
`table1`.`fieldX`,
`table2`.`fieldY`
FROM `db`.`table1`
INNER JOIN `db`.`table2`
ON `table1`.`id` = `table2`.`id`
WHERE `table1`.`id` = ?
ORDER BY `table1`.`timestamp`
DESC LIMIT 1
table1
row_id
id
fieldX
timestamp
table2
row_id
id
fieldY
timestamp
Both tables can have repeating ids. It was designed this way to store older versions of the data entries.
For example: table1 can have 3 rows with the same id while table2 can have 2 rows of the same id. I want to get the latest row from both tables.
Use can use it like this if you want recent record from table2
SELECT SUBSTRING_INDEX(GROUP_CONCAT(t1.fieldX ORDER BY t1.timestamp DESC),',',1) as field_x, SUBSTRING_INDEX(GROUP_CONCAT(t2.fieldY ORDER BY t2.timestamp DESC),',',1) as field_y
FROM table1 t1
JOIN table2 t2 ON(t2.id = t1.id)
GROUP BY t1.id
ORDER BY t1.timestamp

Joining two table and including all rows even with out matches

Looking for away to join 2 tables and keeping all records from the left. The problem is that one of the on statement does not exist in the the other table
Example
Table1 ID, ClassID, Date, Amount
Table2 ID, CLassID, Date, Score
Example statement
select
Table1.ID,
Table1.ClassID,
Table1.Date,
Table1.Amount,
Table2.Score
from Table1
left join Table2 on Table2.ID = Table1.ID
and Table2.ClassID = Table1.ClassID
and Table2.Date = Table1.Date
The Problem is Table1 has an ClassID that Table2 does not have and it is excluding that from the results. If I removed the ClassID as a qualification, I get a ton of duplicates
Your missing value will be null for Score so you will have to deal with that however you like, but this will do the job.
select
Table1.ID,
Table1.ClassID,
Table1.Date,
Table1.Amount,
( SELECT Score
FROM Table2
WHERE ID = Table1.ID
and ClassID = Table1.ClassID
and Date = Table1.Date ) AS 'Score'
from Table1

Use result from SQL query in different table

If I have three different tables like this
table_1
Field 1: victories
Field 2: name
table_2
Field 1: name
Field 2: birthday
Now I would want to get the birthday of the person with the most victories.
So I would do something like this (pseudo code):
select victories from table_1 and sum_it_all
get name and pass name to table_2
select birthday from table_2 where name
Ok, this is pretty ugly pseudo code, but I hope you get the point.
Using Andomar's solution works fine.
Now I tried to nest another table in it, like this though:
select address
from table_3
where birthday =
(
select birthday
from table_2
where name =
(
select name
from table_1
group by
name
order by
sum(victories) desc
limit 1
)
)
I do get a correct answer, but for some reason also get a null back. And how would I output the sum of victories?
select birthday
from table_2 t2
where name =
(
select name
from table_1 t1
order by
victories desc
limit 1
)
If one user can have multiple rows in table_1, you'd have to sum the victories:
select birthday
from table_2 t2
where name =
(
select name
from table_1 t1
group by
name
order by
sum(victories) desc
limit 1
)
I think what you're looking for is something like this:
SELECT t2.name, SUM(t1.victories) as SumOfVictories, t2.birthday
FROM table_1 as t1
JOIN table_2 as t2
ON table_1.name = table_2.name
GROUP BY t2.name, t2.birthday
ORDER BY SUM(t1.victories) DESC
LIMIT 1
You can use following nested SQL:
select name, birthday from table_2 where name in (
select name from table_1 order by victories desc limit 1
)
Would SELECT * FROM table_1 INNER JOIN table_2 ON table_1.name=table_2.name ORDER BY victories DESC give you the desired results?
This might be a solution for your problem:
SELECT table_1.name, table_2.birthday
FROM table_2
JOIN table_1 ON table_1.name=table_2.name
WHERE table_1.victories>=ALL(SELECT table_1.victories
FROM table_1)
try this:
Select name, birthday from table_2 t2
Join table_1 t1 On t1.Name = t2.name
Having Count(*) =
(Select Max(namCount)
From (Select Count(*) namCount
From table_1
Group By name))
Group By t1.name