I have simple database with two tables.
In teams table there are columns team and id.
players table has columns player_name, player_age, position and team_id.
team_id in players table is foreign key that refers to team_name in teams table.
I want to display this information in HTML table using PHP where I will have header with team_name, player_name and player_age.
I need only unique team_name values because I want to echo whole table using while loop so I need them to only echo once in header. I hope this makes sense.
My problem is, I don't know how to write a query which will return distinct team_name values from teams table but all players and age info from players table.
I've tried using joins but I always end up having same number of team_name values as player_name values.
I do this for exercise only so any security issues and stuff like that are not my concern right now.
assuming you want show the relation for the team with id = 3 you can use a join as
select t.team_name, p.player_name, p.player_age
from teams t
inner join players p on p.team_id = t.id
and t.id = 3
or if you want the relation based on team name
select t.team_name, p.player_name, p.player_age
from teams t
inner join players p on p.team_id = t.id
and t.team_name = 'your_preferred_team_name'
if you want all team in a single query then
select t.team_name, p.player_name, p.player_age
from teams t
inner join players p on p.team_id = t.id
Order by t.id
So I've got some major tables and a junction table which link a couple of the tables together in MySQL. The first major table is contacts, the 2nd is addresses and the 3rd is categories. The junction tables are contact_address which links contacts and addresses using their contact_id and address_id primary keys. I need to get the contacts with or without addresses which belong to a category called plumber...
I've currently got this:
SELECT
*
FROM
contacts T1
JOIN
contact_categories T2
USING
(contact_id)
JOIN
contact_address T3
USING
(contact_id)
JOIN
addresses T4
ON
T3.address_id = T4.address_id
WHERE
T2.general = 'Plumber'
Which works fine as long as the Plumber has an address, but what about the homeless plumbers... where are they?
You have to use a LEFT JOIN instead of an (INNER) JOIN:
SELECT
*
FROM
contacts T1 JOIN contact_categories T2
USING (contact_id)
LEFT JOIN
contact_address T3
USING (contact_id)
LEFT JOIN
addresses T4
ON T3.address_id = T4.address_id
WHERE
T2.general = 'Plumber'
An INNER JOIN returns all rows where the join operation succeeds, a LEFT JOIN returns all rows from the LEFT table, and the rows from the right table where the join succeeds, or all NULL values for the right table when the join does not. Please have a look at this visual explanations of joins.
I have a table for comments ("event_comments") to different events with the following columns:
post_id
event_id
username
comment
date
I want to be able to retrieve this info from the database and also be able to print the username, first name and last name; for this, I thought of using INNER JOIN, but it is not working for the following reason: I have 3 different profile types (3 different tables) "students", "guardians", "teachers" and when I try to use the INNER JOIN using "username" I get an error message saying that Column 'username' in from clause is ambiguous.
SELECT event_comments.post_id, event_comments.event_id, event_comments.username, event_comments.comment, event_comments.date,
students.first_name, students.last_name, students.picture,
guardians.first_name, guardians.last_name, guardians.picture,
teachers.first_name, teachers.last_name, teachers.picture
FROM event_comments
INNER JOIN students
INNER JOIN guardians
INNER JOIN teachers
USING (username)
ORDER BY date DESC
LIMIT 20
I tried to do this and it worked, but it only shows 1 comment per user; if the user has more than 1 comment then the info is ignored:
SELECT event_comments.post_id, event_comments.event_id, event_comments.username, event_comments.comment, event_comments.date,
students.first_name, students.last_name, students.picture,
guardians.first_name, guardians.last_name, guardians.picture,
teachers.first_name, teachers.last_name, teachers.picture
FROM event_comments
INNER JOIN students
INNER JOIN guardians
INNER JOIN teachers
GROUP BY username
ORDER BY date DESC
LIMIT 20
Does anybody how to get the INNER JOINs to work? is there a better way to do what I want? I hope I explained myself well.
Thanks!
do it like this:
SELECT event_comments.post_id, event_comments.event_id, event_comments.username, event_comments.comment, event_comments.date,
students.first_name, students.last_name, students.picture,
guardians.first_name, guardians.last_name, guardians.picture,
teachers.first_name, teachers.last_name, teachers.picture
FROM event_comments
INNER JOIN students
on event_comments.username=students.username
INNER JOIN guardians
on event_comments.username=guardians.username
INNER JOIN teachers
on event_comments.username=teachers.username
ORDER BY date DESC
LIMIT 20
This will work but assuming that a username from one table is not present in other tables, this will result into 0 rows.
a more logical approach would be to select each table then union it to join every result set like this :
SELECT e.post_id, e.event_id, e.username, e.comment, e_comments.date,
s.first_name, s.last_name, s.picture
from event_comments e
inner join students s
on e.username=g.username
UNION SELECT e.post_id, e.event_id, e.username, e.comment, e_comments.date,
g.first_name, g.last_name, g.picture
from event_comments e
inner join guardians g
on e.username=g.username
UNION SELECT e.post_id, e.event_id, e.username, e.comment, e_comments.date,
t.first_name, t.last_name, t.picture
from event_comments e
inner join teacher t
on e.username=t.username
EDIT:
To explain better about the query it just does this simple steps:
Query all comments from students using username to join post to students
Query all comments from guardians using username to join post to guardians
Query all comments from teachers using username to join post to teachers
Join results from students,guardians, teachers together
You need the using clause for each pair of joins:
FROM event_comments INNER JOIN
students
USING (username) INNER JOIN
guardians
USING (username) INNER JOIN
teachers
USING (username)
In MySQL, an inner join with no on clause is treated as a cross join. In other databases, an on or using clause is required for an inner join.
i'm working with php mysql and there are 12 tables which contains student informations.There are 3 main table First table is registration, second is demandraft and third is creditcard.The demandraft table contains all the creditcard table fields but as empty. now i want to get the whole data from these three tables to generate my xls file but coz there are empty fields of creditcard table in demandraft table so unable to fetch the whole records from all 3 tables. there is stuid field common in all 3 tables.
Here is my join query for that:
$sql = "select * from registration
join programme on registration.id=programme.stuid
join family on registration.id=family.stuid
join address on registration.id=address.stuid
join education on registration.id=education.stuid
join extradetail on registration.id=extradetail.stuid
join workexperience on registration.id=workexperience.stuid
join demanddraft on registration.id=demanddraft.stuid
join payonline on registration.id=payonline.stuid
where (DATE(registration.createddate)>='".$term1."'
AND DATE(registration.createddate)<='".$term2."')";
Use a left join.
select *
from a join b on a.id = b.a_id
will not list lines of table a that do not appear in table b.
select *
from a left join b on a.id = b.a_id
will.
Left join might be a bit tricky when chaining many of them with multiple tables. You may have to cleverly use parentheses around joins such that the order of joins is correct.
This :
$sql = "select * from registration
left join programme on registration.id=programme.stuid
left join family on registration.id=family.stuid
left join address on registration.id=address.stuid
left join education on registration.id=education.stuid
left join extradetail on registration.id=extradetail.stuid
left join workexperience on registration.id=workexperience.stuid
left join demanddraft on registration.id=demanddraft.stuid
left join payonline on registration.id=payonline.stuid
where (DATE(registration.createddate)>='".$term1."'
AND DATE(registration.createddate)<='".$term2."')";
Should do the trick
So i encountered this problem when I was specifying a query for 2 connected tables with more than 1 id.
Suppose there are 2 tables:
Table Competition
p1_id(varchar)(FK)
p2_id(varchar)(FK)
p3_id(varchar)(FK)
table Competition refers to table Player by the player.id
Table Player
id (PK)
name
level
gender
The problem is I want to retrieve the p1 name, p2 name and p3 name from table Competition...
join the table player thrice to get it's equivalent values,
SELECT a.*,
b.name Player1_Name,
c.name Player2_Name,
d.name Player3_Name
FROM Competition a
INNER JOIN player b
a.p1_ID = b.ID
INNER JOIN player c
a.p2_ID = c.ID
INNER JOIN player d
a.p3_ID = d.ID
If one of the columns in table competition is nullable, better use LEFT JOIN than INNER JOIN.
To fully gain knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins