This question already has answers here:
What is the difference between JOIN and UNION?
(14 answers)
Closed 3 years ago.
How can I union multiple table in mysql and order by date?
I have 3 differents tables and I want to make a query to get all data inside that and order by date
SELECT * FROM table1, table2, table3 ORDER by date DESC
TABLE 1
ID | USER ID | DATE |
2 4 2018
TABLE 2
ID | CAR ID | DATE |
3 9 2017
TABLE 3
ID | AIR ID | DATE |
4 6 2019
I expected result like this
ID | ALLID | DATE
2 4 2018
3 9 2017
4 6 2019
The correct syntax for union is union/union all. A comma in the from clause is not correct syntax . . . well, for anything.
So:
select id, allid, date from table1
union all
select id, allid, date from table2
union all
select id, allid, date from table3
order by date desc;
You would not be able to create your 2nd column ALLID, however you could use Join to create the tables, but the search results would be difficult if you are trying to search by ID. Can you give more context.
TO Join Table 1 and Table 2 together you need to have a primary key and a foreign key, so a column from table 1 and a cloumn from table 2 that both appear in table 3.
SO you can use ID from Table 1 and Date from table 2 because both are in table 3 so these are your Join accessories:
basic join: all data
SELECT * FROM table1 t1 JOIN table3 t3 JOIN table2 t2
bit more detail: (using on as a where clause to filder based on specific date)
SELECT * FROM table1 t1 JOIN table3 t3 JOIN table2 t2 ON t3.DATE=t2.DATE
Related
My tables look like this. my op and country is having many to many relationships with each other.
OP
id, name,.....
op_country
id, op_id, country_id
country
id, name, ...
my op_country filled like below
id op_id country_id
1 1 1
2 1 2
3 2 2
4 2 3
5 3 3
6 3 3
7 1 1
I want to remove my duplicate entries from op_country. Here I want to remove rows 6 and 7 since we already have rows with such values.
How can I do that.
DELETE t1
FROM op_country t1
JOIN op_country t2 USING (op_id, country_id)
WHERE t1.id > t2.id
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=247ebc5870a6ab10b64076ffb375797f
You want to delete entries for which exists a sibling with a lower ID:
delete from op_country
where exists
(
select null
from (select * from op_country) op2
where op2.op_id = op_country.op_id
and op2.country_id = op_country.country_id
and op2.id < op_country.id
);
The from (select * from op_country) is necessary instead of a mere from op_country due to some weird restriction in MySQL updates.
I would like to select multi col across multi table in order to display them as only one row in MYSQL
like this example:
___________
|uId|xID|yID|
|_1_|_2_|_4_|
Into
___
|zId|
| 1 |
| 2 |
| 4 |
here is the query but col t returns null data beside all of the selected cols
SELECT `flaghsip_leader`,`clustuer_coordinator_id`,`clustuer_cocoordinator_id`,
`flagship_activity_coleader_id`,`flagship_activity_focalpoint_id`,
`output_leader_id`,`output_coleader_id` AS `t`
FROM crpcoreix.view_all_involved_users;
From what you've described, I think you'll need to use multiple select queries and join them with UNION ALL.
e.g.
SELECT uId as zId from Table1
UNION ALL
SELECT xId as zId from Table2
UNION ALL
SELECT zId from Table3
This question already has answers here:
How can I SELECT rows with MAX(Column value), PARTITION by another column in MYSQL?
(22 answers)
Closed 6 years ago.
Let there be two tables:
Table A
id | name
1 x
2 y
Table B
foreign_key | value | external
1 1 60
1 2 50
2 3 80
2 4 90
The desired result is a JOIN looking like this:
id | name | external
1 x 50
2 y 90
i.e., for each row in A we get the corresponding external from B where value is max for a given id.
What I have so far is this:
SELECT
A.`id`,
A.`name`,
B.`external`
FROM `A`
LEFT JOIN `B`
ON A.id = B.foreign_key
GROUP BY id
This obviously returns the first B.external encountered instead of the one with the highest value:
id | name | external
1 x 60
2 y 80
Is there a way to achieve this, preferably without using subqueries?
Not sure why dont want sub-query but Correlated sub-query looks simpler to me
select id, name,
(Select external
from TableB B where A.id = B.foreign_key Order by Value desc Limit 1 )
From TableA A
If you want to achieve this using JOIN then you may have to join the TableB twice
I got 2 tables.
TABLE 1
ID FRANCHISENAME TELEPHONE FRANCHISE_ID
1 BURGER 666-555-999 5
2 JSUBS 666-555-999 7
3 STEAKS 777-888-999 3
TABLE 2
ID NAME TELEPHONE EMAIL FRANCHISE_ ID
5 JOHN 555-444-333 JOHN#GMAIL.COM 5
5 JOHN 555-444-333 JOHN#GMAIL.COM 7
6 EDGARD 555-444-333 EDGARD#GMAIL.COM 9
I want to retrieve all data in table one, except for that data where the user has his email in Table 2. As for example JOHN has franchise_id 5 and 7, so the query would only return
3 STEAKS, 777-888-999, 3
Assuming that TABLE_1 & TABLE_2 relate to each other through TABLE_1.FRANCHISE_ID & TABLE_2.FRANCHISE_ID
You can use NOT EXISTS
SELECT
*
FROM TABLE_1 T1
WHERE NOT EXISTS(
SELECT *
FROM TABLE_2 T2
WHERE T2.FRANCHISE_ID = T1.FRANCHISE_ID
AND T2.EMAIL = 'JOHN#GMAIL.COM'
)
OR
You can use LEFT JOIN along with IS NULL
SELECT
T1.*
FROM TABLE_1 T1
LEFT JOIN TABLE_2 T2 ON T1.FRANCHISE_ID = T2.FRANCHISE_ID
WHERE T2.FRANCHISE_ID IS NULL;
SELECT t1.*
FROM
Table1 t1
LEFT JOIN Table2 t2
ON t1.FRANCHISE_ID = t2.FRANCHISE_ID
AND LEN(IFNULL(t2.EMAIL,'')) > 0
WHERE
t2.ID IS NULL
Even if there is a record in Table 2 if it has no email it will be returned by this query. You can expand say to > 7 or more to check for a minimum level of validity of email address.
This should get you there using the NOT IN function. It will exclude records from Table 1 if there is a matching Franchise ID in Table 2, unless the email field is Table 2 is null:
SELECT * FROM Table1
WHERE Table1.Franchise_ID NOT IN
(SELECT Table2.Franchise_ID FROM Table2
WHERE Table2.Email IS NOT NULL);
My SQL Skills are next to none. After looking around for the past 2 hours trying to figure this out I need some help please.
I have 2 tables as below
Table1 Table2
ID | Name Status_id
----------- ----------
1 | Open 1
2 | Closed 2
3 | On-Hold 1
What I would like to do is count the status_id in table 2 and group by the status_id. Then add the Name where the ID matches in the first column.
What I have at the moment is
SELECT status_id, COUNT(*) AS 'num' FROM table2 GROUP BY status_id
This is great so far and returns
1 | 2
2 | 1
What I need to return is
Open | 2
Closed | 1
I hope that is clear. Can anyone help?
Many thanks!
SELECT a.name, COUNT(*) AS num FROM table2 b
INNER JOIN table1 a
ON b.status_id=a.id
GROUP BY status_id
In the case that you want to also have Zero for On-Hold you'd need to do a LEFT join and count the a column from table2 instead of *
SELECT t1.name,
Count(t2.Status_id) AS num
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.Status_id
GROUP BY t1.name;
DEMO