How do i combine 3 tables in one query in php - php

Hi guys how do i combine this 3 tables in one single query in php?
PHP
<?php
$db_name = "atfest_db";
$mysql_user = "root";
$mysql_pass = "";
$server_name = "localhost";
$sql = "SELECT teamone,teamtwo,s_name FROM sport as A right join matches
as B right join schedule as C ON A.s_id = B.s_id ORDER by m_id asc";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
$result = mysqli_query($con,$sql);
$response = array();
while($row=mysqli_fetch_array($result))
{
array_push($response, array("teamone"=>$row[0], "teamtwo"=>$row[1],
"s_name"=>$row[2]));
}
echo json_encode (array("schedule_response"=>$response));
mysqli_close($con);
?>
Sport table is connected to matches b s_id while matches table is connected to schedule table by m_id
Matches Table
Schedule Table
Sport Table

You can join two or more table using join() in mysql like:
select t1.col1, t2.col2
FROM table1 as t1 JOIN table2 as t2
on t1.some_col = t2.some_col // where t1, t2 have a common column on which joining is done
where condition;
Reference

Try this SQL-statement instead:
SELECT B.teamone,B.teamtwo,A.s_name
FROM sport A
LEFT JOIN matches B
LEFT JOIN schedule C
ON A.s_id = B.s_id
ORDER by C.m_id asc

SELECT teamone,teamtwo,s_name FROM sport A
LEFT JOIN matches B ON A.m_id=B.m_id
LEFT JOIN schedule C ON A.s_id = B.s_id ORDER by A.m_id asc

Hi this is the right query for you . You are getting error because you are not using right query..
SELECT teamone,teamtwo,s_name FROM sport as A right join matches as B ON A.s_id = B.s_id right join schedule as C ON B.m_id = C.m_id order by B.m_id asc
Now you can go through it to get the appropriate result .This query is generated according to your schema.

SELECT `schedule`.`id`, (and other relevant fields from schedule table), `matches`.`m_id`, `matches`.`teamone`, `matches`.`teamtwo`, (and other relevant fields from matches table), `sport`.`s_id`, (and other relevant fields from sport table)
FROM `sport`
RIGHT JOIN `matches` ON `matches`.`s_id` = `sport`.`s_id`
RIGHT JOIN `schedule` ON `schedule`.`m_id` = `matches`.`m_id`
ORDER BY `schedule`.`id`
The above query will solve your basic question of combining three tables. But this is a bad practice. There will be a lot of redundant data. You're selecting only three column. In that, there is no index also. You can't figure out how each row is different from one another. Please mention the purpose or the logic of the query. So that we can fine tune the query accordingly.
Update
I've updated the columns which we are selecting in the query. Now there is a unique column schedule id to uniquely identify each row. You can select other columns also depends upon the necessity.

Related

mysql join where for 1 table

I have 2 tables, student and grades
student table contains id, name and date_of_birth
grades table contains id, student_id, grade and course
Actual table contain more data.
I have a query like
SELECT s.*, AVG(g.grade) as average_grade
FROM student s LEFT JOIN grade g ON s,id = g.student_id
WHERE g.course = 'mathematics' and s.id = 1
With this I could get the data i needed which are student details and the average grade, then come the problem where when the course = "mathematics" is not found in the grades table, the query will return NULL. My question is, is there a way for me to get the s.id = 1 details together with NULL average instead of all NULL value?
I would prefer if it is able to do it with 1 query, as because in my current I am using subquery and it takes very long to get the data. My main objective is to get more faster speed if you have better way instead of using 1 query feel free to comment your idea. In addition I have tried multiple query and sub query to get all the data but it all take too long.
Move your filter criteria for g.course = 'mathematics' in joining part
SELECT s.*, AVG(g.grade) as average_grade
FROM student s
LEFT JOIN grade g ON s.id = g.student_id AND g.course = 'mathematics'
WHERE s.id = 1
Your query produces result as inner join not left because putting g.course = 'mathematics in where clause turns your left join to inner join, Moving this part in on clause will still return data from student table if there were no rows found from grade table with course = 'mathematics'
If the course is not 'mathematics' you would still get the student data if you put it like this.
SELECT s.*, AVG(g.grade) as average_grade
FROM student s LEFT JOIN grade g ON s,id = g.student_id
WHERE (g.course = 'mathematics' AND s.id = 1) OR s.id = 1

I would like to expand a SQL query to also get the name of the ID's it is selecting. How do I do that?

At the moment I have a very basic SQL query, however I would like to expand it in order to fetch the names of the ID's it is referencing as well. The problem is, it's a Parent > Child relationship table. The Parent and Children items are part of the same table, but different rows.
$GetRelations = "
SELECT RelationUID, BlockTypeUID, ChildBlockUID, Weight FROM block_type_relations
WHERE BlockTypeUID = '$BlockTypeUID'
";
When I only have a single BlockTypeUID to initially fetch the records, how can I shift this query around so that it also fetches the names from table block_types for both BlockTypeUID and ChildBlockUID? I'm writing this in mySQLi.
Assuming the name is in a column named "name", the query could look like this
$GetRelations = "
SELECT a.RelationUID, a.BlockTypeUID, a.ChildBlockUID, a.Weight
b.NAME AS BlockTypeName, c.NAME AS ChildBlockName
FROM block_type_relations a
INNER JOIN block_types b ON b.ID = a.BlockTypeUID
INNER JOIN block_types c ON c.ID = a.CildBlockUID
WHERE a.BlockTypeUID = '$BlockTypeUID'
";
$GetRelations = "SELECT btr.RelationUID, btr.BlockTypeUID, btr.ChildBlockUID,
btr.Weight,bt.Name FROM block_type_relations btr
INNER JOIN block_types bt ON bt.BlockTypeUID =btr.BlockTypeUID
WHERE BlockTypeUID = '$BlockTypeUID'";

complex mysql Statement

I am having a little issue here.
So I have two tables and I need to fetch data in what i think is a complex way.
So below is a summary of the two tables
clients
client_id
name
booked [default is 0]
accommodation
accommodation_id
client_id
date
price
What I would like to have is select all client id's from tbl
clients where booked is 1
then using the client_ids select all rows in accommodation whose
client_id is an of those returned in step 1
What i had in mind proved difficult for me
$select_accomodation = "SELECT * FROM `accommodation` WHERE `booked` = 1";
if($select_accomodation_run = #mysql_query($select_accomodation))
{
//awesome code that does no 2
}
What is the best possible way to accomplish tasks 1 and 2. Hopefully in one mysql statement
If you just want to select all accommodations for booked clients you could do
SELECT a.*
FROM accommodation a
INNER JOIN clients c ON a.client_id = c.client_ID
WHERE c.booked = 1
Try this:
select t1.client_id, t2.accommodation_id, t2.client_id, t2.data, t2.price from clients t1 JOIN accommodation t2 on t1.client_id = t2.client_id WHERE t1.booked = 1
My thought, is first write a subquery that gets the Ids you want for part 1, which is:
SELECT client_id FROM clients WHERE booked = 1
Then, you can use that subquery inside another query for the accomodations table using the IN clause
SELECT a.* FROM accomodation a WHERE a.client_id IN (SELECT c.client_id FROM clients c WHERE c.booked = 1);

MySQL selecting from multiple tables

im trying to select data from multiple tables, now i am fine with doing it with two tables as i do a query like so:
$myquery = sql_query(
"SELECT a.object_title, a.published_by, b.userid
FROM table1 AS a
JOIN table2 AS b ON (a.published_by = b.userid)"
);
But see now, i want to select data from a third table, however this third table does not have relationship such as primary key between the first two tables, so i simply want to just pull data from it and form any sort of link with a "JOIN".
How would simply add the third to this query?
Thanks
You could use CROSS JOIN :
$myquery = sql_query(
"SELECT a.object_title, a.published_by, b.userid, c.whatever
FROM table1 AS a
JOIN table2 AS b ON (a.published_by = b.userid)
CROSS JOIN table3 AS c"
);
I used this other post to find the idea.
More infos here.
Add within you query a Left Join.
$myquery = sql_query(
"SELECT a.object_title, a.published_by, b.userid, c.column_name
FROM Table1 AS a
JOIN Table2 AS b ON (a.published_by = b.userid)
CROSS JOIN Table3 AS c"
);

Query Issue - LEFT JOINS where specific col is match to variable

I`m fetching data from sql with left join query, I want to filter the query to be more specific,
In my database i have customers, every customer have a Group, every user have his customers. i guess you understand now what i need.
every user will see only the rows that relate to him. this is my query:
SELECT t.*,c.fname,e.DISCODE,e.AREA,e.COLOR
FROM $tbl_name AS t
LEFT JOIN customers AS c ON t.MCcode = c.MCcode
LEFT JOIN eventcodes AS e ON t.MCcode = e.MCcode AND t.CODE=e.CODE
ORDER By `id` DESC LIMIT $start, $limit
ON customers table i have field named Group, and when user is connected i have his Group name, so how i can filter this query that only where ( for example ) c.Group = '$Group', all the rows are found in $tbl_name, and the other details i`m fetching by joins.
thanks!
Like this
LEFT JOIN customers AS c ON t.MCcode = c.MCcode and c.Group = '$Group'

Categories