Retrieve data from tables having many-to-many relationships? - php

Now how i select the project_title from table2 having tm_id=10
and what is the best way to do this task?

SELECT t2.project_title FROM table2 AS t2
JOIN table3 AS t3 ON t3.project_id = t2.project_id
WHERE t3.tm_id = 10;

I think a simple INNER JOIN will suffice your need.
SELECT a.*, c.project_title
FROM Online_team a
INNER JOIN team_project b
ON a.tm_id = b.tm_id
INNER JOIN online_team_projects c
ON b.project_ID = c.project_ID
WHERE a.tm_id = 10
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
But if you don't need any columns from Online_team, you can remove it from the joins list.
SELECT c.project_title
FROM team_project b
INNER JOIN online_team_projects c
ON b.project_ID = c.project_ID
WHERE b.tm_id = 10

use the query as below
SELECT table2.project_title from table2,table3 where table2.project_id = table3.project_id and table3.tm_id = 10

SELECT
otp.project_title
FROM online_team_projects otp,
team_project tp
WHERE otp.project_id = tp.project_id
AND tp.tm_id = 10

You could use this approach:
SELECT otp.project_title
FROM online_team ot
INNER JOIN online_team_projects otp USING (project_id)
WHERE ot.tm_id = 10

Related

Having issues with this Mysql query

Hello i'm learning sql and i have some issues with joins(which i have problems understanding them)
I have this issue
#1066 - Not unique table/alias: 'tbl_respuestas'
what the query supposed to do, is count how many people(general,ignore user) has answer 'x', in 'y' question of 'z' survey
SELECT COUNT(*) FROM tbl_respuestas
INNER JOIN tbl_encuesta_usuario ON tbl_encuesta_usuario.user_id = user.id
INNER JOIN tbl_encuesta ON tbl_encuesta.id = tbl_encuesta_usuario.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas ON tbl_encuesta_has_tbl_preguntas.tbl_encuesta_id = tbl_encuesta.id
INNER JOIN tbl_preguntas ON tbl_preguntas.id = tbl_encuesta_has_tbl_preguntas.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas ON tbl_preguntas_has_tbl_respuestas.tbl_preguntas_id = tbl_preguntas.id
INNER JOIN tbl_respuestas ON tbl_respuestas.id = tbl_preguntas_has_tbl_respuestas.tbl_respuestas_id
WHERE tbl_respuestas.respuesta = 2
line SELECT COUNT(*) FROM tbl_respuestas
and line INNER JOIN tbl_respuestas
does not makes sense, hence the error.
Unless it is what you want then you need to give then different name/alias like below:
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_respuestas r2
Also as a quick note you can rewrite the entire sql like below.
It is good practice to give your tables a name for shorter referencing and makes the sql look a little cleaner.
Also if both tables you are trying to join has the same column name then you can use the keyword USING instead of having to write that long line tbl_encuesta_usuario.user_id = user.id
Please be sure to put r and r2 in its prope place
SELECT COUNT(*) FROM tbl_respuestas r
INNER JOIN tbl_encuesta_usuario u USING user_id
INNER JOIN tbl_encuesta e ON e.id = u.tbl_encuesta_id
INNER JOIN tbl_encuesta_has_tbl_preguntas hp ON hp.tbl_encuesta_id = e.id
INNER JOIN tbl_preguntas p ON p.id = hp.tbl_preguntas_id
INNER JOIN tbl_preguntas_has_tbl_respuestas hr ON hr.tbl_preguntas_id = p.id
INNER JOIN tbl_respuestas r2 ON r2.id = hr.tbl_respuestas_id
WHERE r.respuesta = 2

MySQL INNER JOIN query issue

I have created an INNER JOIN query as shown below and was wondering how I can make it work? I need for the HomeTeam and AwayTeam to equal TeamID in the query. Any help would be much appreciated. Thanks
$result = mysqli_query($con,"SELECT results.*,team.TeamName
FROM results
INNER JOIN team ON team.TeamID = results.HomeTeam
INNER JOIN team on team.TeamID = results.AwayTeam");
You need to use aliases for the table that you are including twice. Otherwise mysql cannot distinguish between the two.
To be able to process the results easily, you can do the same with the names you are selecting.
Something like:
SELECT
results.*,
t1.TeamName AS TeamNameHome,
t2.TeamName AS TeamNameAway
FROM results
INNER JOIN team t1
ON t1.TeamID = results.HomeTeam
INNER JOIN team t2
ON t2.TeamID = results.AwayTeam

How to Select from four tables in sql?

This is the table structure
plinks
link
ojectidfk
uidfk
ojects
ojectid
uidfk
ojectname
Try
useridfk
ojectidfk
goryidfk
gory
goryid
goryname
What i want to do is select the ojectname from ojects where the plinks projectidfk is the same in ojects and plinks but select everything from try where the ojectid is equal to the ojectsid where pinks link = 8493284 AND gory id = try goryidfk
Select obj.objectnamem, try.* from objects as obj
inner join plinks on plinks.projectidfk = obj.ojectid
inner join try on try.projectidfk = obj.ojectid
inner join gory on gory.goryid = try.goryidfk
where plinks.link = 8493284
Try joining all four tables and use where clause for link like:
SELECT o.objectName, t.*
FROM ojects o INNER JOIN plinks p ON o.ojectId = p.ojectidfk
INNER JOIN try t ON o.ojectid = t.ojectidfk
INNER JOIN gory g ON g.goryid = t.goryidfk
WHERE p.link = 8493284
Using simple JOIN should do this for you. You just connect tables by constraints and then retrieve whatever you want from any of those tables.
Please notice, that if you write a pseudo-code it would look pretty similar to the actual code. You need to use INNER JOINs because you want to be sure, that there are rows that share all those relations and match your criteria on plinks.link.
SELECT o.ojectname, t.*
FROM
ojects o
INNER JOIN plinks p ON p.ojectidfk = o.ojectid
INNER JOIN try t ON t.ojectidfk = o.ojectid
INNER JOIN gory g ON g.goryid = t.goryidfk
WHERE
p.link = 8493284

Join but return ALL records from Table

I have the following SQL query:
SELECT * FROM `table1` INNER JOIN `table2` ON table1.messageid=table2.messageid WHERE `venue_active` = 1
The above works fine but it only returns fields where both tables have a messageid field.
My problem is that I need it to return ALL fields from Table1 reguardless if it has a messageid match in table2 or not.
So, in other words I need ALL records to be returned from Table1 and all records from Table2 where there's a messageid that matches both.
How can I do this?
Use a LEFT JOIN rather
SELECT *
FROM `table1` LEFT JOIN
`table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
That said, it will only work if venue_active is also part of table1, and not table2.
Have a look at the different scenarios
SQL Fiddle DEMO
Use a LEFT join rather than INNER
For example:
SELECT * FROM `table1`
LEFT JOIN `table2` ON table1.messageid=table2.messageid
WHERE `venue_active` = 1
Either you need a LEFT JOIN instead, or
a FULL OUTER JOIN workaround for MySQL:
SELECT
a.*,
b.*
FROM
table1 a
LEFT JOIN
table2 b ON a.messageid = b.messageid
WHERE a.venue_active = 1
UNION
SELECT
a.*,
b.*
FROM
table1 a
RIGHT JOIN
table2 b ON a.messageid = b.messageid;
WHERE a.venue_active = 1

how do i join 7 tables where a column (which exists in all tables) equals the same content in all tables?

i have seen how to inner join 2 tables where a column is equal to the content in another column. but how do i do this with 7 tables?'
thanks everyone,
I figured it out lol after a long time. this seems to work
SELECT *
FROM
tbl_school
INNER JOIN tbl_apprequirments ON (tbl_school.schoolname = tbl_apprequirments.schoolname)
INNER JOIN tbl_citygallery ON (tbl_apprequirments.schoolname = tbl_citygallery.schoolname)
INNER JOIN tbl_schoolgallery ON (tbl_citygallery.schoolname = tbl_schoolgallery.schoolname)
INNER JOIN tbl_livingexp ON (tbl_schoolgallery.schoolname = tbl_livingexp.schoolname)
INNER JOIN tbl_tuition ON (tbl_livingexp.schoolname = tbl_tuition.schoolname)
where tbl_school.schoolname = 'glendale community college';
SELECT * FROM t1 JOIN t2 JOIN t3 JOIN t4 JOIN t5
ON (t2.c=t1.c AND t3.c=t1.c AND t4.c=t1.c AND t5.c=t1.c)
MySQL provides a shorthand for this:
SELECT * FROM t1 JOIN (t2, t3, t4, t5)
ON (t2.c=t1.c AND t3.c=t1.c AND t4.c=t1.c AND t5.c=t1.c)
This example is for 5 tables. You can repeat as necessary.
See MySQL's join syntax.
Edit: after seeing the clarification from sarmenhb, I think this query also works:
SELECT * FROM t1 JOIN (t2, t3, t4, t5, t6, t7)
USING (schoolname)
WHERE t1.schoolname = 'name'
After joining 2 of them, join the thrid to the first two, then the 4th to the first 3, etc,
Select *
From T1 Join T2 On <criteria>
Join T3, on <criteria>
Join T4 On <Criteria>
etc...
Try something like this...
Select data
from table as tbl1
join as tbl2 on tbl2.data = tbl1.data
join as tbl3 on tbl3.data = tbl1.data
is this what you are looking for?

Categories