PHP/MySQL select query to go through four related tables in database - php

I have a select query and an insert query as follows:
$select_query = mysql_query("SELECT trees, animals FROM Table1 WHERE gardens > '10000'", $mysql_connection);
while ($row = mysql_fetch_array($select_query)) {
$trees = $row['trees'];
$animals = $row['animals'];
$names = ?????????? // in order to get the names, a select query should go through Table2 and Table4
//and finally get the assigned names from Table3. Please see the schematic picture of tables.
$insert_query = mysql_query("INSERT INTO table6 (new_trees, new_animals, new_name) VALUES ('$trees', '$animals', '$names')", $mysql_connection); }
I want to select and insert the $trees, $animals and $names into another table. There is no problem with $trees and $animals variables, but I do not know how to select the data for $names. As it is seen in the schematic picture of tables, Table1.id=Table2.reference and Table2.first_id gets values from Table4.id.
Then, Table4.second_id gets values from Table3.id and finally Table3.name must be selected in order to satisfy the $names variable in above-mentioned $insert_query. Sorry if I did not explain the problem more clearly. Could you please review the picture and let me know your solution?

Lookup the JOIN clauses:
SELECT
t1.id, t1.trees, t1.animals, t3.name
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t2.reference = t1.id
LEFT JOIN table4 AS t4
ON t2.first_id = t4.id
LEFT JOIN table3 AS t3
ON t4.second_id = t3.id
WHERE // Your select conditions

Related

Mysql 2 tables use player_id with name from other table

I am new to php & mysql and I'm trying to make a script that gets the distance walked with the player's name. I can get the player's walked distance with his id, but the value for the player_id is in a different table.
It looks like this:
Table1: player_id | foot (walked distance)
Table2: name | player_id
So I want to use the name by the player_id in my table.
Code
You require a simple join.
SELECT Table1.foot, Table2.name
FROM Table1
INNER JOIN Table2
ON Table1.player_id = Table2.player_id;
You just need to join both these table.
Just try this code:
$query = "SELECT T1.*, T2.name
FROM table1 T1
LEFT JOIN table2 T2 ON T1.player_id = T2.player_id
ORDER BY T2.name ASC";
For more details of JOIN: Link
Let me know for more help.
You can use
$query = "select t1.player_id, t2.name, t1.foot
from table1 t1
join table2 t2 on t1.player_id = t2.player_id"
If you want to order the player names in alphabetical order then you can additionally use order by clause
$query = "select t1.player_id, t2.name, t1.foot
from table1 t1
join table2 t2 on t1.player_id = t2.player_id
order by t2.name"
Use left join in mysql.
Suppose if you have two tables use this
SELECT T1.*,T2.walked distance
FROM table1 T1
LEFT JOIN table2 T2
ON T1.id=T2.player_id;
Click Here For more example

Multiple select statements in one query

I can do the following in 2 queries but want to make it simpler. Can this be combined in one query? If so how more efficient is it than doing two queries vs one?
query1: SELECT page_id, coupon_id from table_1 WHERE key = :key
query2: SELECT folder from table_2 WHERE page_id = table_1.page_id
For my final result I need to have a coupon_id from table_1, and a folder from table_2.
In query2 I need to use the page_id result from query1 to get the folder
Is there a simpler way to do this?
Use JOIN (LEFT, RIGHT or INNER is up to your needs):
SELECT
t1.page_id,
t1.coupon_id,
t2.folder
FROM
table_1 AS t1
LEFT JOIN table_2 AS t2 ON
t2.page_id = t1.page_id
WHERE
t1.key = :key
You will want to JOIN the tables on the page_id:
SELECT t1.page_id,
t1.coupon_id,
t2.folder
from table_1 t1
inner join table_2 t2
on t1.page_id = t2.page_id
WHERE key = :key
If you need help learning join syntax, here is a great visual explanation of joins.
I used an INNER JOIN which will return all rows that match between the two tables. If you want to return all rows from table_1
even if it doesn't have a matching row in table_2, then you would use a LEFT JOIN
SELECT
table_1.coupon_id AS coupon_id,
table_2.folder AS folder
FROM
table_1
INNER JOIN table_2 ON table_2.page_id = table_1.page_id
WHERE
table_1.key = :key
SELECT t1.page_id, t1.coupon_id, t2.folder
FROM table_1 t1 LEFT JOIN table_2 t2 ON (t1.page_id = t2.page_id)
WHERE t1.key = :key
This will be faster than two queries, how much depends on your data.
Try this please:
SELECT a.page_id, a.coupon_id, b.folder_id
from table_1 a
join table_2 b
ON a.page_id = b.page_id
WHERE a.key = :key
group by a.page_id
;

How echo data from 3 different databases after selection?

Have 3 tables called "table1" "table2" and "table3". Only the column id is same in these tables. All other columns are different.
Will select like:
"select * from table1, table2, table3 where WHERE (date >= now())";
I am facing problem while echoing the data. The table1 have one column called "org", table2 have "name", table3 have "pgm". I want something like:
echo "$data['org']" OR $data['name'] OR $data['pgm']"
Is this possible?
So, assuming what you said is true:
Only the column id is the same in these tables
Then you can join the tables that share this column value and retrieve the values back in a query:
SELECT t1.org,
t2.name,
t3.pgm
FROM table1 t1
INNER JOIN table2 t2
ON t2.id = t1.id
INNER JOIN table3 t3
ON t3.id = t1.id
WHERE t1.date >= NOW();
An explicit join, but further illustrates the relationship the tables must have when collecting data from different sources.

Selecting data from mysql table and related data from another to join them

Ive looked at other questions and answers but still dont understand which brings me here.
I have one data base two tables. lets say table1 and table2 from database.
I'm looking to grab all the information from table1 and only one column from table2 that coincides with the correct row in table1.
Example which I know is wrong:
SELECT table1.*, table2.time_stamp FROM table1, table2
WHERE table1.ticket_id=$var AND table1.user_id = table2.user_id
Basically select data from table1 then use a value from the selected table to grab the related data from table2 and join them to output them as one mysql_query. Im sure its simple and has been asked before.
edit:
I dont receive an error. SQL just returns noting. log form of this would be:
$sqlResults = mysql_query("SELECT table1.* FROM table1 WHERE table1.ticket_id=$var")
while($rowResult = mysql_fetch_array( $sqlResults )) {
$userID = $rowResult['user_id'];
$sqlResults2 = mysql_query("SELECT table2.time_stamp FROM table2
WHERE table2.user_id=$userID")
}
I want to combine that into one sql statement so i dont have to hit table2 for every row table1 has
Use a JOIN to bind the rows from table2 to those from table1:
SELECT t1.*, t2.time_stamp FROM table1 t1
JOIN table2 t2 ON t1.user_id = t2.user_id
WHERE t1.ticket_id=$var

Help with grasping (INNER?) JOIN

I'm having trouble building a query. I can do what I want in 3 different queries.
SELECT id FROM table1 WHERE url LIKE '%/$downloadfile'
put that in $url_id
SELECT item_id FROM table2 WHERE rel_id = '$url_id'"
put that in $item_id
SELECT rel_id FROM table2 WHERE rel_id = '$item_id' AND field_id = '42'"
put that in $user_id
But from reading examples on joins and inner joins I think there's a more elegant way. I cant wrap my brain around writing a better query (but would like to) I can describe how it should go:
table1
fields: id, url
table2
fields item_id, rel_id, field_id
I know the last part of table1.url (LIKE '%/$filename') with that I select table1.id.
table1.id is equal to one entry in table2.rel_id. So get that and select the table2.item_id.
In table2 there is another entry which has the same table2.item_id and it will have a table2.field_id = '42'
And finally the value I need is the table2.rel_id where the table2.field_id was 42.
I will fetch that value and put it in $user_id
Can this be done with one query using joins/inner joins?
SELECT url, second.rel_id AS user_id
FROM table1
INNER JOIN table2 AS first
ON table1.id=first.rel_id
INNER JOIN table2 AS second
ON first.item_id=second.rel_id
WHERE second.field_id='42'
AND table1.url LIKE '%/:downloadfile'
Sure, should be something like:
SELECT t2_second.rel_id
FROM table1 t1
INNER JOIN table2 t2_first ON t1.id = t2_first.rel_id
INNER JOIN table2 t2_second ON t2_second.rel_id = t1_first.item_it
WHERE
t1.url = '%/:filename' AND
t2_second.field_id = 42
You could even throw in a distinct so that each t2_second.rel_id is only returned once:
SELECT DISTINCT [...]
You might not need this, however, if t2_second.field_id = 42 will restrict the query to only return one row for each t2_second.rel_id
This the query that works for me, made one small change to Ignacio's answer:
SELECT url, second.rel_id AS user_id
FROM table1
INNER JOIN table2 AS first
ON table1.id=first.rel_id
INNER JOIN table2 AS second
ON first.item_id=second.item_id
WHERE second.field_id='42'
AND table1.url LIKE '%/:downloadfile'

Categories