I have 2 tables
matchdata:
id_team1, id_team2, name_team1, name_team2, group_order_id
teams:
team_id, team_name
I try this:
select * from matchdata join teams on matchdata.id_team1 = teams.team_id;
But I need
matchdata.id_team1 = teams.team_id
AND
matchdata.id_team2 = teams.team_id
after that I want select WHERE group_order_id = $bla
How can I do that?
You can use two JOIN on a single table.
SELECT * FROM matchdata
JOIN teams t1 ON matchdata.id_team1 = t1.team_id
JOIN teams t2 ON matchdata.id_team1 = t2.team_id
WHERE group_order_id = $bla;
You can use IN for this:
SELECT *
FROM matchdata md
JOIN teams t ON t.team_id IN (md.id_team1, md.id_team2)
WHERE md.group_order_id = $bla
Related
I have Two tables :
T1
"IdT1;i1T1,n2T1
1;123;n2T1
2;234;n3T1
3;345;n4T1
4;678;n1T1
5;123;n2T1
6;234;n3T1
T2
"idT2;n1T2;n2T2;i1T2
1;1n1T2;2n1T2;123
2;1n2T2;2n1T2;234
3;1n3T2;2n1T2;345
4;1n4T2;2n1T2;456
5;1n5T2;2n1T2;567
6;1n6T2;2n1T2;678
In SQLITE3
$req = $db -> prepare("SELECT T2.n1T2, T2.n2T2,
//COUNT(*)
FROM T1
INNER JOIN T2 ON T1.idT2 = T2.idT2
");
if ($res = $req->execute()) {
$arr = $res->fetchArray();
var_dump($arr);
}
I search for n1T2 and n2T2 names, the count of same values between i1T1 and i1T2
example:
1n1T2;2n1T2;2
1n2T2;2n2T2;2
1n3T2;2n1T2;1
...
I var_dump for display, in wait best method
It looks like you want a join and aggregation:
select t2.n1t2, t2.n2t2, count(*) cnt
from t1
inner join t2 on t2.i1t2 = t1.i1t1
group by t2.n1t2, t2.n2t2
$query = "SELECT * FROM table3 WHERE name_id = '(SELECT name_id FROM table2
WHERE salary < 1000 && name = '(SELECT name FROM table1
WHERE savings > 1000)')'";
Basically I want to get the data from the table1 based on the savings and use it to get the data from table 2 and use that data to get all the information from table 3. But this wont work. Is my code right or am I doing something wrong?
I also cannot create new tables, I simply want to display the data from table 3.
Use join
SELECT * FROM table3 t3 join table2 t2
on t3.name_id=t2.name_id
join table1 t1
on t3.name=t1.name
where salary < 1000 and savings > 1000
$query="SELECT * FROM table3 LEFT JOIN table2 ON table3.name_id=table2.name_id
LEFT JOIN table1 ON table3.name=table1.name
WHERE table2.salary < 1000 AND table1.savings > 1000 "
Another syntax of join is
SELECT * FROM table1 t1,table2 t2 ,table3 t3
where t1.name = t3.name and
t2.name_id = t3.name_id and
t1.savings > 1000 and t2.salary < 1000;
$query = "SELECT t3.* FROM table3 t3
INNER JOIN table2 t2 ON t2.name_id = t3.name_id AND t2.salary < 1000
INNER JOIN table1 t1 ON t1.name = t2.name AND t1.savings > 1000";
In a table if I have:
FixtureID, HomeTeam, AwayTeam
Can I replace the ID that is related to HomeTeam and AwayTeam when it displays in the browser? HomeTeam and AwayTeam are both related to TeamID in the Teams table.
I want to show all fixtures and then replace both "TeamID" with "TeamName" so the name shows up instead of the ID?
So far I have:
$sql = <<<SQL
SELECT fix.*, tea.*
FROM Fixtures fix
INNER JOIN Teams tea USING (TeamID)
SQL;
Then
echo '<p>Fixtures</p>';
echo '<div>'.$row['HomeTeam'].' v '.$row['AwayTeam'].'</div>';
EDIT:
Ok so I found a post that was similar to what I need and have tried the following:
$sql = <<<SQL
SELECT fix.*, tea1.*, tea2.*
FROM Fixtures fix
INNER JOIN Teams tea1 ON fix.HomeTeam = tea1.TeamID
INNER JOIN Teams tea2 ON fix.AwayTeam = tea2.TeamID
SQL;
However it's still just showing the team ID's rather than the names from the Teams table.
Solved it myself
$sql = <<<SQL
SELECT f.*, t1.TeamName 'HomeTeam', t2.TeamName 'AwayTeam'
FROM Fixtures f
INNER JOIN Teams t1 ON f.HomeTeam = t1.TeamID
INNER JOIN Teams t2 ON f.AwayTeam = t2.TeamID
SQL;
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
I have drawn this image to explain what I need
1.to compare a user_id with the user_id's in two different tables
2.the corresponding ref_global_id from both the tables are then matched to a events table
3.matching global_id's from the events table are then arranged in ascending order.
Or this:
SELECT e.global_id, e.event_time
FROM (SELECT * FROM table1
UNION
SELECT * FROM table2) x inner join
event_table e ON e.global_id = x.ref_global_id
WHERE x.[user_id] = 121
SELECT e.global_id, e.event_time
FROM events_table e
JOIN table1 t1 on e.global_id = t1.ref_global_id
JOIN table2 t2 on e.global_id = t2.ref_global_id
WHERE t1.user_id = 121 AND t2.user_id = 121
ORDER BY e.event_time
Try this:
select global_id, event_time from event left join table1 on event.global_id = table1.ref_global_id AND table1.user_id = 121 left join table2 on event.global_id = table2.ref_global_id AND table2.user_id = 121