mysql join 3 tables - php

How can I join three mysql tables which have one common column (id), For example, Select a, b from Table1, select c,d from table2, select e,f from table3, where id=x
Thanks

SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM table1 t1
JOIN table2 t2 ON (t1.id = t2.id)
JOIN table3 t3 ON (t1.id = t3.id)
ORDER BY t1.id;

SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM `table1` t1
JOIN `table2` t2 ON t1.id = t2.id
JOIN `table3` t3 ON t1.id = t3.id
WHERE t1.id = x

SELECT `table1`.`a`,`table2`.`c` .....
FROM `table1` JOIN `table2` USING(`id`) JOIN `table3` USING(`id`)
WHERE `id` = x

SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM table1 t1, table2 t2, table3 t3
WHERE t1.id = t2.id
AND t2.id = t3.id
AND t3.id = x

SELECT col1,col2,col3 (select any col from any table )
FROM t1 INNER JOIN t2,t3
WHERE t1.id = t2.id
AND t1.id = t3.id;

Please try this query:
SELECT product_details.product_id, product_name.pro_name,categories.cat_name
FROM product_details
INNER JOIN product_name
ON product_details.product_id=product_name.id INNER JOIN categories ON product_details.categories_id=categories.id order by product_details.id;

Related

COUNTING mysql table rows based on 2 conditions from another table

I have two tables table1 and table2. table1 has columns id and table2_id while table2 has id and category. I need to count rows from table1 based on two separate values in table2.category containing value Regular or Special.
I have done this in two queries but I want to know if it is possible in a single sql. My queries are:
"SELECT COUNT(t1.id) AS regular FROM table1 t1 LEFT OUTER JOIN table2 t2 ON t1.t2_id = t2.id WHERE t2.category = 'Regular'";
"SELECT COUNT(t1.id) AS special FROM table1 t1 LEFT OUTER JOIN table2 t2 ON t1.t2_id = pr.id WHERE t2.category = 'Special'";
Thanks.
EDIT
The second query JOIN should read ON t1.t2_id = t2.id and not ON t1.t2_id = pr.id. Sorry for the confusion that may have caused. Please update/edit your answers/comments accordingly.
Move the Where condition to CASE statement and do the counting
Here is one way using Conditional Aggregate
SELECT
COUNT(case when t2.category = 'Regular' then t1.id end) AS Regular,
COUNT(case when t2.category = 'Special' then t1.id end) AS special
FROM table1 t1
INNER JOIN table2 t2 ON t1.t2_id = pr.id
Where t2.category IN ('Regular','Special' )
Note : I have changed the LEFT JOIN to INNER JOIN because you want to count only when table2.category is 'Regular' or 'Special' so no use of LEFT JOIN here
Instead of
"SELECT COUNT(t1.id) AS regular FROM table1 t1 LEFT OUTER JOIN table2 t2 ON t1.t2_id = t2.id WHERE t2.category = 'Regular'";
"SELECT COUNT(t1.id) AS special FROM table1 t1 LEFT OUTER JOIN table2 t2 ON t1.t2_id = pr.id WHERE t2.category = 'Special'";
you can do this:
select t2.category, count(t1.id)
from table1 t1
left outer join table2
on t1.t2_id = t2.id
group by t2.category
having t2.category in ('Regular', 'Special')
The suggested query groups the joined records, filters the groups and selects the category name and its count.

Connect to multiple tables

How can I connect to 4 tables in a single query using forign key IDs?
I know how to connect to two tables.
$sql = "SELECT tb1.id, tb2.name FROM tblA tbl1 LEFT JOIN tblB tbl2 ON tb1.id = tbl2.studentID ORDER BY tbl1.id DESC LIMIT 20";
$statement = $con_db->prepare($sql);
Try like this :
select t1.ID, t2.studentID, t3.aID, t4.bID
from table1 as t1
left join tbl2 as t2 on t2.studentID = t1.id
left join tbl3 as t3 on t3.aID = t1.id
left join tbl4 as t4 on t4.bID = t1.id

select from table only if the id exist in that table mysql pdo php

I want to create a select query using join on 3 or more tables. I have 3 tables namely t1, t2, t3 and a common column id existing in all 3 tables. I want to select the 3 table if the id exists in the table my query is like this.
Select * from t1
inner join t2 on t1.id = t2.id
inner join t3 on t2.id = t3.id
where t1.id = 1 and t2.id = 1 and t3.id = 1
the query is returning values if the id exists in all the 3 tables. But if it is not in any table example t3 i will not return anything. I am looking for a way that if it does not exist in t3 it should i proceed to just select from t1 and t2
Is this what you need?
SELECT *
FROM t1
INNER JOIN t2 on t1.id = t2.id
LEFT JOIN t3 on t2.id = t3.id
WHERE t1.id = 1 AND t2.id = 1 AND (t3.id = NULL OR t3.id = 1)
SELECT * FROM t1 JOIN t2 ON t1.id = t2.id
LEFT JOIN t2 ON t1.id= t2.id WHERE t1.id= 1
This will return null for t3 columns if id is not present in t3
I think these sql wil helpful to you
Select * from t1,t2,t3
where t1.id = 1 or t2.id = 1 0r t3.id = 1
these sql also will useful to you
SELECT * FROM t1 WHERE t1.id=1
UNION ALL
SELECT * FROM t2 WHERE t2.id=1
UNION ALL
SELECT * FROM t2 WHERE t2.id=1
Thank you.

Complicated MySQL Query taking forever to execute

I've a somewhat suboptimal database structure I have to work with. Table1 contains 2 Ids (lets call them id1, id2). These Ids link to 2 rows in another table Table2. I want to get some columns of Table1 and use id1 and id2 to get columns out of Table2. Do I really have to join the same table twice to link to different rows or is there another more efficient way to do this? My queries take over 30 seconds for 20 rows.
SELECT t1.id1, t1.id2, t2.name, t3.name
FROM Table1 t1, Table2 t2, Table2 t3
WHERE t1.id1 = t2.id AND t1.id2 = t3.id AND t1.index = 2
Not tested, but it should be possible to use JOINS:
SELECT t1.id1, t1.id2, t2.name FROM
FROM Table1 t1
JOIN Table2 t2
ON t1.id1 = t2.id OR t1.id2 = t2.id
WHERE t1.index = 2
If you really need this Many-to-Many relation, please try JOIN:
SELECT t2.id AS t2_id,
t3.id AS t3_id,
t2.name AS t2_name,
t3.name AS t3_name
FROM Table1 t1
JOIN Table2 t2
ON t1.id1 = t2.id
JOIN Table3 t3
ON t1.id2 = t3.id
WHERE t1.index = 2

I need a good mysql query to receive information from 3 tables

I have this function:
function view_user_anunt($user) {
$query="SELECT * FROM `anunturi`
FULL OUTER JOIN tranzactie
ON anunturi.tranzactie = tranzactie.id_tranzactie WHERE `anunturi.user`=:code";
$stmt = $this->dbh->prepare($query);
$stmt->bindParam(':code', $user, PDO::PARAM_INT);
$stmt->execute();
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $result)
{
$view[]="
<tr>
<td>".$result['id_anunt']."</td>
<td>".$result['den_tranzactie']."</td>
<td>".$result['den_proprietate']."</td>
<td><a href='#' id='vizualizare'>Select</a></td>
<td><a href='#' id='modificare'>Select</a></td>
</tr>";
}
return $view;
}
and 3 tables:
anunturi
id_anunt (int) auto increment
tranzactie(int)
tip (int)
user(int)
tranzactie
id_tranzactie (int) auto increment
den_tranzactie varchar
tip
id_proprietate (int) auto increment
den_proprietate varchar
I need a good query or an ideea to get transaction name (den_tranzactie) and proprietate name den_proprietate for each row from anunturi where anunturi.user = $user.
Thanks in advance...
Try this sql query
$query="SELECT t1.*, t2.*, t3.*
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.foreightkey_id
LEFT JOIN table3 t3 ON t1.id = t3.foreightkey_id
WHERE t1.user=:code
";
You can use INNER JOIN to receive only the rows witch have data in all tables. for example
$query="SELECT t1.*, t2.*, t3.*
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.foreightkey_id
INNER JOIN table3 t3 ON t1.id = t3.foreightkey_id
WHERE t1.user=:code
";
or if you want get all data from table1 and table2 which are connected and from table 3 only that which are connected
$query="SELECT t1.*, t2.*, t3.*
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.foreightkey_id
LEFT JOIN table3 t3 ON t1.id = t3.foreightkey_id
WHERE t1.user=:code
";
Something like this?
'
SELECT t.den_tranzactie, tip.den_proprietate
FROM
anunturi a
JOIN tranzactie t ON a.tranzactie = t.id_tranzactie
JOIN tip ON a.tip = tip.id_proprietate
WHERE a.user =:code

Categories