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
Related
here us the query
SELECT *
FROM Table1
WHERE complete='Y'
AND shipped='Y'
AND active='Y'
AND create_dttm > '2013-10-10 08:28:41'
AND order_id IN
(SELECT DISTINCT t1.order_id
FROM Table2 t1
INNER JOIN table3 t2 ON t1.prod_id = t2.prod_id
WHERE t2.prod_sku LIKE '%D-600%'
AND t1.create_dttm > '2013-02-15 08:28:41')
You are using a sub-query in WHERE clause, that could be the main reason behind slow execution of your query. Try using JOINS instead of sub query.
SELECT t1.*
FROM Table1 t1
INNER JOIN Table2 t2 ON T1.order_id = T2.order_id
AND t2.create_dttm > '2013-02-15 08:28:41'
INNER JOIN table3 t3 ON t2.prod_id = t3.prod_id
AND t3.prod_sku LIKE '%D-600%'
WHERE complete='Y'
AND shipped='Y'
AND active='Y'
AND create_dttm > '2013-10-10 08:28:41'
And also check for indexes on your 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
I am currently using this script below.
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
WHERE table2.user_id = $userid");
The tables have these fields:
Table1:
id, venue_id, user_id...
Table2:
id, venue_id, user_id...
The query above returns 5 records.
Now....
I need to add a third table to the above script Table3
Table 3 fields also contains id, venue_id, user_id... BUT I don't what it in the WHERE of the script.
I've tried adding a LEFT JOIN to the script above to add the third table like this:
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
LEFT JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid");
The Stats table only contains 1 record.
Now, my problem is that the query above it's echoing the data on ALL the records and not just the one.
My question is...What I'm I doing wrong on the line I added:
LEFT JOIN stats as table3 on table1.venue_id = table3.venue_id ?
OK, I think you want to also join on the user_id. So
SELECT
*
FROM
`venues` AS table1
LEFT JOIN `follows` AS table2 USING (venue_id)
LEFT JOIN `stats` AS table3 USING (venue_id, user_id)
WHERE
table2.user_id = $userid
is my solution
If you only want to include record from table1 that have non null records in table3 then you need to use INNER JOIN and not a LEFT JOIN. See the MySQL documentation for JOIN
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
INNER JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid");
The "INNER" is not needed explicitly. Joins are INNER joins by default
You're not limiting the records at all. Use this instead:
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
LEFT JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid AND table3.venue_id IS NOT NULL");
You can use an INNER JOIN rather than an LEFT JOIN. See this SO post to make it clear or this blog article
This is the code I have :
$sqlz = "SELECT t1.user_id, t2.status, t2.email
FROM coverages t1
LEFT JOIN users t2 ON t1.user_id = t2.user_id
GROUP BY t1.user_id
HAVING COUNT(t1.user_id) =".$value;
I would like to add this "WHERE users.email IS NOT NULL"
When I do add it, it returns a white page / no results. which I know for a fact there are at least 200 results on the db that contain an email and and match that criteria.
this is an example of what I did that did not work:
$sqlz = "SELECT t1.user_id, t2.status, t2.email
FROM coverages t1
LEFT JOIN users t2 ON t1.user_id = t2.user_id
WHERE users.email IS NOT NULL
GROUP BY t1.user_id
HAVING COUNT(t1.user_id) =".$value;
I think you need to use t2 (alias) instead of users.
$sqlz = "SELECT t1.user_id, t2.status, t2.email
FROM coverages t1
LEFT JOIN users t2 ON t1.user_id = t2.user_id
WHERE t2.email IS NOT NULL
GROUP BY t1.user_id
HAVING COUNT(t1.user_id) = " .$value;
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;