Combine multiple sql / mysql queries in one query - php

I have 5 tables. Namely user, reference_1, reference_2, reference_3 and question
user => (user_id,user_name,user_emailid,user_mobno)
reference_1 => (ref_id_1,user_id,ref_name,ref_email,ref_mobno)
reference_2 => (ref_id_2,user_id,ref_name,ref_email,ref_mobno)
reference_3 => (ref_id_3,user_id,ref_name,ref_email,ref_mobno)
question => (que_id,user_id,ref_id_1,ref_id_2,ref_id_3,que1_name,que2_name,que3_name,que4_name,que5_name,que6a_name,que6b_name,que7_name,qa1,qa2,qa3,qa4,qa5,qa6a,qa6b,qa7,flag)
I fired 4 consecutive queries:
Query 1: Fetching the data from the user table and question using a LEFT JOIN
select u.user_name,u.user_emailid,u.user_mobno,
q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7
from user u
LEFT JOIN question q
on u.user_id = q.user_id
WHERE q.flag = 1
Query 2: Fetching the data from the user table and reference_1 using LEFT JOIN
select r1.ref_name,r1.ref_email,r1.ref_mobno,
q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7
from reference_1 r1
left join question q
on r1.ref_id_1 = q.ref_id_1
Query 3: Fetching the data from the user table and reference_2 using LEFT JOIN
select r2.ref_name,r2.ref_email,r2.ref_mobno,
q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_nam e,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7
from reference_2 r2
left join question q
on r2.ref_id_2 = q.ref_id_2
Query 4: Fetching the data from the user table and reference_3 using LEFT JOIN
select r3.ref_name,r3.ref_email,r3.ref_mobno,
q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7
from reference_3 r3
left join question q
on r3.ref_id_3 = q.ref_id_3
Now there is a requirement to combine all 4 queries in one to show the data.
I tried JOIN all these queries in one query
select u.user_name,u.user_emailid,u.user_mobno,
q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,
r1.ref_name,r1.ref_email,r1.ref_mobno, q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,
r2.ref_name,r2.ref_email,r2.ref_mobno, q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,
r3.ref_name,r3.ref_email,r3.ref_mobno, q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q.que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7
from user u
LEFT JOIN question q
on u.user_id = q.user_id
WHERE q.flag = 1
LEFT JOIN reference_1 r1
LEFT JOIN question q1
on r1.ref_id_1 = q1.ref_id_1
LEFT JOIN reference_2 r2
LEFT JOIN question q2
on r2.ref_id_2 = q2.ref_id_2
LEFT JOIN reference_3 r3
LEFT JOIN question q3
on r3.ref_id_3 = q3.ref_id_3
But this query is not working.

Because you screwed syntax order...
Multi join works like this:
select u.user_name,u.user_emailid,u.user_mobno, q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q. que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,
r1.ref_name,r1.ref_email,r1.ref_mobno, q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q. que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,
r2.ref_name,r2.ref_email,r2.ref_mobno, q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q. que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7,
r3.ref_name,r3.ref_email,r3.ref_mobno, q.que1_name,q.qa1,q.que2_name,q.qa2,q.que3_name,q.qa3,q.que4_name,q.qa4,q.que5_name,q.qa5,q. que6a_name,q.qa6a,q.que6b_name,q.qa6b,q.que7_name,q.qa7
from question q
LEFT JOIN user u USING(user_id)
LEFT JOIN reference_1 r1 USING(ref_id_1)
LEFT JOIN reference_2 r2 USING(ref_id_2)
LEFT JOIN reference_3 r3 USING(ref_id_3)
WHERE q.flag = 1
Where must be at the end...
Joins must be stated completely and cannot be mixed. Order in dependency works you can have sub and subsub and so on dependencies without a problem.
One thing you did pretty good: You set-up same name for foreign key in both JOIN tables.
In this case you can simply replace r1.t1 = r2.t2 stuff and just define the join table in function USING(t1). :)

Try this query. Uses nested join to the reference tables onto the question table.
SELECT u.user_name
,u.user_emailid
,u.user_mobno
,q.que1_name
,q.qa1
,q.que2_name
,q.qa2
,q.que3_name
,q.qa3
,q.que4_name
,q.qa4
,q.que5_name
,q.qa5
,q.que6a_name
,q.qa6a
,q.que6b_name
,q.qa6b
,q.que7_name
,q.qa7
,r1.ref_name
,r1.ref_email
,r1.ref_mobno
,r2.ref_name
,r2.ref_email
,r2.ref_mobno
,q.que1_name
,r3.ref_name
,r3.ref_email
,r3.ref_mobno
FROM dbo.user u
LEFT JOIN question q
LEFT JOIN reference_1 r1 ON r1.ref_id_1 = q.ref_id_1
LEFT JOIN reference_2 r2 ON r2.ref_id_2 = q.ref_id_2
LEFT JOIN reference_3 r3 ON r3.ref_id_3 = q.ref_id_3 ON u.user_id = q.user_id
WHERE q.flag = 1

Related

how write sql query with multiple join?

I have fours tables and I wanted to join all three tables with the one table.
I have listed my problem as follows:
Tables:
users
user_training
user_courses
user_certificates
I wanted to get the data from [2,3,4] tables that user_id field matches with the users table ID field.
When I try the INNER JOIN it gives me the result for users that are common in all the tables, But I just wanted to check the [2,3,4] tables with the table [1] Records.
My Query...
SELECT A.training_name AS 'training_name', C.course_name AS 'course_name', D.certificate_name AS 'certificate_name'
FROM user_training AS A INNER JOIN users AS B ON A.user_id=B.ID INNER JOIN user_courses AS C ON B.ID = C.user_id INNER JOIN user_certificates AS D ON B.ID = D.user_id;
Thanks in Advance.
use left join
select u.* from users u
left join user_training ut on ut.user_id=u.user_id
left join user_courses uc on uc.user_id=u.user_id
left join user_certificates uct on uct.user_id=u.user_id
With this one you are getting all users and their respective trainings:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
Changing *_trainig to *_courses or *_certificates will return all users with respected courses or certificates.
If you need to get data in one query, try this one:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
LEFT JOIN `user_courses` ON `users`.`id` = `user_courses`.`user_id`
LEFT JOIN `user_certificates` ON `users`.`id` = `user_certificates`.`user_id`
If user has no trainings, courses, certificates all remaining fields will be null-ed.

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

LEFT JOIN of 2 tables query is Failing

I am trying to get the left outer join of 2 tables with respect to two tables , but i am unable to execute this query , phpmyadmin is giving #1064 error on line 12 when running this query:
SELECT
pt.id as planid,
pt.trip_name,
pt.description,
cor.latitude,
cor.longitude,
bb.id as bookmarkid,
bb.num_of_persons as persons
FROM
planned_trips as pt,
coordinates as cor,
LEFT JOIN Bookmarkedby as bb,Users as user
ON
user.id = 1 AND
user.id = bb.user_id AND
bb.plannedtrips_id = pt.id AND
pt.coordinates_id = cor.id'
I've struggling for an hour , what am i missing??
my database schema looks like this:
I am Currently just preparing my query i need to run this query on codeIgnitor.
You need a separate LEFT JOIN for each table.
SELECT
pt.id as planid,
pt.trip_name,
pt.description,
cor.latitude,
cor.longitude,
bb.id as bookmarkid,
bb.num_of_persons as persons
FROM planned_trips as pt
INNER JOIN coordinates as cor ON pt.coordinates_id = cor.id
LEFT JOIN Bookmarkedby as bb ON bb.plannedtrips_id = pt.id
LEFT JOIN Users as user ON user.id = bb.user_id AND user.id = 1
Problem is in the line LEFT JOIN Bookmarkedby as bb,Users as user. Consider changing it to
FROM
planned_trips as pt
JOIN coordinates as cor ON pt.coordinates_id = cor.id
LEFT JOIN Bookmarkedby as bb ON bb.plannedtrips_id = pt.id
LEFT JOIN Users as user ON user.id = bb.user_id
AND user.id = 1

php select query unknown column in field list?

I'm having an error in mysql query (Error Code : 1054
Unknown column 'p.post_id' in 'field list')
post_id is present in the post table
can any one help me in this issue
SELECT u.iname , p.post_id,p.file_path
FROM users u
INNER JOIN likes l
ON u.user_id=l.user_id
INNER JOIN notify n
ON p.post_id=n.post_id
INNER JOIN post p
ON p.user_id=u.user_id
WHERE u.user_id=3 AND n.notify=1
Tables are not joined in the correct order, you cant reference post_id in the second join as the posts table isn't joined yet.
SELECT u.iname, p.post_id, p.file_path
FROM users u
INNER JOIN likes l ON u.user_id = l.user_id
INNER JOIN post p ON u.user_id p.user_id
INNER JOIN notify n ON p.post_id = n.post_id
WHERE u.user_id = 3 AND n.notify = 1

mysql query for getting related data from different table for suggested connections

we are developing portal like social network site with different concept...but for doing suggested connections we got strucked in mysql queries...
we are trying to take users with similat data ...
SELECT u.*
FROM educonnect_user u
LEFT OUTER JOIN educonnect_user_qualification q ON u.id = q.user_id
LEFT OUTER JOIN educonnect_user_contact a1 ON u.id = a1.user_id
WHERE q.type_of_institution in
(
SELECT type_of_institution
FROM educonnect_user_qualification qi
WHERE qi.user_id = 3
)
AND q.college in
(
select college
from educonnect_user_qualification qc
where qc.user_id = 3
)
AND q.country in
(
select country
from educonnect_user_qualification qco
where qco.user_id = 3
)
AND a1.country in
(
select country
from educonnect_user_contact cc
where cc.user_id = 3
)
AND a1.state in
(
select state
from educonnect_user_contact cs
where cs.user_id = 3
)
Like this I am joining 10 tables ..but the problem is wherever i gave AND operator no result generated and if i gave OR operator it returns all users ..its the logic these operator will give output which I know..but for this problem i need different suggestions which would work effectively..or else the query can be changed with any other specific operator???
You didn't need all these WHERE IN conditions. You just need where educonnect_user.user_id = 3 since the educonnect_user table is joined with the other two tables educonnect_user_qualification and educonnect_user_contact, therefore the join will insure that for the user with id=3 the fields: type_of_institution, college, country,..( and other fields ) are exists in the two other tables for the same user, But you need to care about what type of join you need depending on what fields you want to select from your tables whether Left, right. So I think the following query what you are looking for:
SELECT u.*
FROM educonnect_user u
LEFT OUTER JOIN educonnect_user_qualification q ON u.id = q.user_id
LEFT OUTER JOIN educonnect_user_contact a1 ON u.id = a1.user_id
where u.user_id = 3
Hope this will help ::
SELECT u.*
FROM educonnect_user u
LEFT OUTER JOIN educonnect_user_qualification q ON u.id = q.user_id
LEFT OUTER JOIN educonnect_user_contact a1 ON u.id = a1.user_id
left join educonnect_user_qualification qi on (q.type_of_institution=qi.type_of_institution and qi.user_id=3)
left join educonnect_user_qualification qc on (q.college=qc.college and qc.user_id=3)
left join educonnect_user_qualification qco on (q.country=qco.country and qco.user_id=3)
left join educonnect_user_contact cc on (a1.country=cc.country and cc.user_id=3)
left join educonnect_user_contact cs on (a1.state=cs.state and cs.user_id=3)

Categories