After SELECT a table GROUP BY a field, I get a results, and then I LEFT JOIN the results with a table. Can I do that?
Thankyou!
You should be, try:
Select *
From(
SELECT *
FROM myTableOne
GROUP BY myCol1
) firstTable
LEFT JOIN myTableTwo secondTable ON
firstTable.idColumn = secondTable.idColumn
Where myTableOne is the group by selection and myTableTwo is the table you want to left join on
Related
I am trying to join two tables and get the count and grouped by specific field. However, it outputs same count values even if the other table consist only two rows. How should I fix this?
Here's my code:
SELECT tbl1.preferredDay, COUNT(tbl1.preferredDay) as count_1, COUNT(tbl2.preferredDay) as count_2
FROM tblschedule as tbl1
LEFT JOIN tblappointments as tbl2 ON (tbl1.preferredDay = tbl2.preferredDay)
WHERE tbl1.preferredDay = tbl2.preferredDay
GROUP BY preferredDay;
Here is the output but it should be [15, 0][3, 3]
Your query is based on left join it will return the same count().
This is a working query for Mysql 8:
with tbl1 as (
SELECT preferredDay, count(1) as count_1
FROM tblschedule
GROUP BY preferredDay
),
tbl2 as (
SELECT preferredDay, count(1) as count_2
FROM tblappointments
GROUP BY preferredDay
)
select t1.preferredDay, t1.count_1, t2.count_2
from tbl1 t1
inner join tbl2 t2 on t1.preferredDay = t2.preferredDay
There are two WITHs to get separately the count and then an INNER JOIN to join those results
For Mysql 5.7 and lower :
select t1.preferredDay, t1.count_1, t2.count_2
from (
SELECT preferredDay, count(1) as count_1
FROM tblschedule
GROUP BY preferredDay
) as t1
inner join (
SELECT preferredDay, count(1) as count_2
FROM tblappointments
GROUP BY preferredDay
) as t2 on t1.preferredDay = t2.preferredDay
How to search three different tables with three different columns? The current command:
$sql="select t1.brand_name,t2.category_name from brand_data_add AS t1
LEFT JOIN category_add_data AS t2 ON t1.brand_name=t2.category_name
UNION select t1.brand_name,t2.category_name from brand_data_add AS t1
RIGHT JOIN category_add_data AS t2 ON t1.brand_name=t2.category_name";
SQL:
SELECT
*
FROM
(
SELECT
workouts.name,
workouts.description,
`user`.user_email
FROM
workouts
LEFT JOIN `user` ON
workouts.created_by = `user`.iduser
UNION
SELECT
workouts.name,
workouts.description,
`user`.user_email
FROM
workouts
RIGHT JOIN `user` ON
workouts.created_by = `user`.iduser) AS main_table
WHERE
user_email LIKE '%gmail%';
Explanation:
You should enclose your union query with bracket
Fetch the fields with SELECT clause
Use the WHERE clause to do the conditional filter in virtual table main_table (union of two table)
select t1.brand_name,t2.category_name, t3.? from brand_data_add AS t1
LEFT JOIN category_add_data AS t2 ON t1.brand_name=t2.category_name
LEFT JOIN t3 on t3 on t3.? = t?
UNION select t1.brand_name,t2.category_name, t3.? from brand_data_add AS t1
RIGHT JOIN category_add_data AS t2 ON t1.brand_name=t2.category_name"
RIGHT JOIN t3 on t3 on t3.? = t?
I have one query that returns count and some columns from two tables and another query that returns count and a column from two tables.
I want to combine this two queries that results in single row per id.
i have tried this:
select
(select a.column_1 as ID,a.column_2,COUNT(b.column_2) as Cnt1
from
table_1 a left outer join table_2 b on a.ID=b.ID
group by
a.column_1 as ID,a.column_2
)
where EXISTS
(select a.column_1 as ID,COUNT(c.column_2) as Cnt2
from
table_1 a left outer join table_3 c on a.ID=c.ID
group by
a.column_1
)
without knowing your real schema...based on your sample query, assuming your inner queries are correct (some minor syntax error). You might want to do something like this.
select *
FROM
(select a.column_1 as ID,a.column_2,COUNT(b.column_2) as Cnt1
from table_1 a left outer join table_2 b on a.ID=b.ID
group by a.column_1,a.column_2
) T1
INNER JOIN
(select a.column_1 as ID,COUNT(c.column_2) as Cnt2
from table_1 a left outer join table_3 c on a.ID=c.ID
group by a.column_1
) T2 ON T1.ID = T2.ID
sqlfiddle
You might want to replace select * with select T1.ID,T1.column_2,Cnt1,Cnt2 based on your comment.
Is it possible to join a table inside a table join? If so how do I do it?
I have experience doing standard joins (LEFT JOIN) and have worked out how to join multiple tables but cant see how to join a table inside a table join. Here is my data structure to explain better:
T1 (
T1_T2
)
T1_T2 (
T1_T2_id
T1_T2_T3
)
T1_T2_T3 (
T1_T2_T3_id
T1_T2_T3_a
T1_T2_T3_b
)
Currently my SQL looks like this:
SELECT * FROM T1
LEFT JOIN T1_T2
ON T1.T1_T2 = T1_T2.T1_T2_id
This returns the data I want from T1 and T1_T2. I want to join T1_T2 on to T1_T2_T3 like so:
SELECT * FROM T1_T2
LEFT JOIN T1_T2_T3
ON T1_T2.T1_T2_T3 = T1_T2_T3.T1_T2_T3_id
Can I do this in one query?
I guess you are looking for something like this:
SELECT * FROM T1
LEFT JOIN T1_T2
ON T1.T1_T2 = T1_T2.T1_T2_id
LEFT JOIN T1_T2_T3
ON T1_T2.T1_T2_T3 = T1_T2_T3.T1_T2_T3_id
Trying to do an inner join on two composite tables, having trouble with the syntax. Here's what I have:
SELECT
count(*)
FROM
(
SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID t1
INNER JOIN
(
SELECT DISTINCT UserID FROM Bids, Users WHERE Bids.UserID = Users.userID
)
t2 ON t1.userID = t2.userID
)
I'm guessing it has something to do with the parantheses/lack of as/or whatever. I guess what I'm really asking here is how to give my subqueries aliases, but not using as in the FROM part. Is it valid just to have t1 after User.userID and identify that whole table as t1?
I think this is what you want?
SELECT count(*)
FROM Users
INNER JOIN Items ON Users.userID = Items.seller
INNER JOIN Bids ON Users.UserID = Bids.UserID
You want to name the output table which you get from query
SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID
as t1
simple way is use
`select * from (SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID)t1`