Searching from three different tables with three different columns - mysql

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?

Related

how to combine two queries having count from different tables and more than one columns

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.

SQL 2 Left Join in one query with count

I have a SQL query:
SELECT table1.column1, table2.column2, table1.column2
FROM table1 LEFT JOIN table2 (ON table1.column1=table2.column2)
What I want to do is add another left join to the table but also to count in that left join data like:
SELECT table1.column1, table2.column2, table1.column2, COUNT(table3.column1)
FROM table1 LEFT JOIN table2 ON table1.column1=table2.column2
LEFT JOIN table3 ON table1.column1=table3.column1
the code does not seems to work, what could be wrong?
count is an aggregate function - you can't mix it with single-row functions without a group by clause. One way around this is to join on a subquery instead of directly on table3 and apply the group by there:
SELECT table1.column1, table2.column2, table1.column2, cnt
FROM table1
LEFT JOIN table2 ON table1.column1 = table2.column2
LEFT JOIN (SELECT column1, COUNT(*) AS cnt
FROM table3
GROUP BY column1) table3 ON table1.column1=table3.column1

OR'ing two INNER JOIN's in MySQL

Is it possible to OR two separate INNER JOIN's so that the result set contains data from either of the two INNER JOIN's? For instance, is the following possible in MySQL.
SELECT * FROM table1
(INNER JOIN table2 ON table1.column_name=table2.column_name)
OR
(INNER JOIN table3 ON table1.column_name=table3.column_name)
No you cant do this way, one way is to use union
SELECT * FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name
union
SELECT * FROM table1 INNER JOIN table3 ON table1.column_name=table3.column_name
You can use UNION:
SELECT * FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name
UNION DISTINCT
SELECT * FROM table1 INNER JOIN table3 ON table1.column_name=table3.column_name
As described at https://dev.mysql.com/doc/refman/4.1/en/union.html, UNION combines the results of several selects.
you need use where
select * from table1
where
(table1.column_name = (select table2.column_name from table2 inner join table1.column_name as on table1.column_name = table2.column_name))
OR
(table1.column_name = (select table3.column_name from table3 inner join table1.column_name as on table1.column_name = table3.column_name))
work perfect here!

How to JOIN two tables and map three columns

2 tables table1 and table 2; requirement is to map two columns in table1 from table 2
SELECT , t2.people_names, t2.peoples_names
FROM ( table1 t1 JOIN table2 t2 ON t2.people_ids=t1.people1 )
If I add in the FROM statement and AND, in order to map column people2, I get an error.
FROM (table1 t1 JOIN table2 t2 ON t2.people_ids=t1.people1 AND
t2.people_ids=t1.people2 )​
You have to join the people_names table two times.
SELECT t1.people1, tnames1.peoples_names, t1.people2, tnames2.peoples_names
FROM table1 t1 JOIN table2 tnames1 ON tnames1.people_ids = t1.people1
JOIN table2 tnames2 ON tnames2.people_ids = t1.people2

Combining Left & Right Join in MySQL query

I am joining 2 tables -tbl1 and tbl2. Left join give all data from tbl1 which is in tbl2 or only on tbl1. Right join gives data from tbl2 which is don't exists in tbl1.
I want to combine both results.
What is the best way to do this so that I get all data from tbl1 and tbl2?
The only you can do that is by using UNION. MySQL doesn't support FULL JOINjust like in MSSQL.
SELECT *
FROM tbl1 t1
LEFT JOIN tbl2 t2
ON t1.col = t2.col
UNION
SELECT *
FROM tbl1 t1
RIGHT JOIN tbl2 t2
ON t1.col>= t2.<col
SEE HERE: Simulating FULL JOIN in MYSQL
By the way, UNION has optional keyword ALL,when the ALL is omitted, UNION automatically selects DISTINCT rows from the resultset.
EXAMLE:
SELECT *
FROM tableA
UNION ALL
SELECT *
FROM tableA
this can result duplicates rows
ColA ColB
==================
1 John
2 Jade
2 Jade
3 Hello
BUT if you omit the word ALL
SELECT *
FROM tableA
UNION
SELECT *
FROM tableA
this can result distinct rows only
ColA ColB
==================
1 John
2 Jade
3 Hello
What you want is FULL JOIN
LEFT JOIN + RIGHT JOIN = FULL JOIN
So try this:
SELECT * FROM tbl1
LEFT JOIN tbl2 ON tbl1.id = tbl2.id
UNION
SELECT * FROM tbl1
RIGHT JOIN tbl2 ON tbl1.id = tbl2.id
The UNION clause combines the results of two SQL queries into a single table of all matching rows.
Here's an alternative that can be easily extended if you have a full join of more than 2 tables:
SELECT t1*, t2.*
FROM
( SELECT col
FROM tbl1
UNION
SELECT col
FROM tbl2
) AS d
LEFT JOIN tbl1 AS t1
ON t1.col = d.col
LEFT JOIN tbl2 AS t2
ON t2.col = d.col ;
you have to use FULL OUTER JOIN, But mysql doesnt support it.. You could do this for getting the result:
SELECT *
FROM tbl1 t1 LEFT JOIN tbl2 t2
ON t1.<col> = t2.<col>
UNION
SELECT *
FROM tbl1 t1 RIGHT JOIN tbl2 t2
ON t1.<col>= t2.<col>