php select query unknown column in field list? - php

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

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.

How to perform inner join in mysql

I have to write a query such that ,I need to get events whose start date is of 30 min from now.
My conditions are:
1) get the event from events table
2)Join created by of events with id in users table.
3)Comments from comment table with user ser id
But the problem here is if there is no comment for event then the event it self is not coming.If any comment is present it is coming.I dont want this.If comment is not there just fetch it as empty but not hide the total event .Can anyone please help me,.Thanks.
select u.email ,group_members.user_id,users.first_name,u.first_name
as host_name,events.name,events.start_date,comments.comments,c.first_name as
comment_user,comments.id from events
inner join users as u on u.id = events.created_by
inner join comments on events.id = comments.event_id
inner join group_members on events.group_id = group_members.group_id
inner join users as c on comments.from_user = c.id
inner join users on group_members.user_id = users.id
where events.start_date between date_add(now(),interval 1 minute) and date_add(
now(),interval 30 minute)
and group_members.user_status = 2
and events.status = 2
You need a left join to the comments table. I would put that table last in the from clause.
select u.email, gm.user_id, gu.first_name, u.first_name as host_name,
e.name, e.start_date, c.comments, uc.first_name as comment_user,
c.id
from events e inner join
users u
on u.id = e.created_by inner join
group_members gm
on e.events.group_id = gm.group_id inner join
users gu
on gm.user_id = gu.id left join
comments c
on e.id = c.event_id left join
users uc
on c.from_user = uc.id
where e.start_date between date_add(now(),interval 1 minute) and date_add(now(),interval 30 minute) and
gm.user_status = 2 and
e.status = 2;
Once you use a left join on comments, you also need a left join for the from user. I replaced all table names with aliases -- this makes it easier to track which table is used for which purpose.
Use the INNER JOIN Keyword and select the two columns by putting them with keyword ON.
SELECT EMP.EMP_ID, EMP.EMP_NAME, DEPT.DEPT_NAME FROM EMP
INNER JOIN DEPT ON DEPT.DEPT_ID = EMP.DEPT_ID;

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

MySql Join Query left join

I have table name students having fields student_id, house_id etc..
and subjects, houses, students_subjects
I am using this query
SELECT students_subjects.student_id,students.house_id,students_subjects.subject_id,subjects.subject_name,students.rollno,students.first_name, students.last_name FROM
students_subjects LEFT JOIN students on students_subjects.student_id=students.id
LEFT JOIN subjects on students_subjects.subject_id=subjects.id WHERE students_subjects.class_years_section_id=1
This is working fine for me ..
Now i want to get house name too from houses table
I tried this query
SELECT students_subjects.student_id,students.house_id,houses.house_name, students_subjects.subject_id,subjects.subject_name,students.rollno,students.first_name, students.last_name FROM
students_subjects LEFT JOIN students on students_subjects.student_id=students.id
LEFT JOIN subjects on students_subjects.subject_id=subjects.id
LEFT JOIN houses on students.house_id=houses.id
WHERE students_subjects.class_years_section_id=1
AND students_subjects.school_session_id=1 AND students.is_active=1
and it gives me house_name = NULL
Can anybody tell me how to get house name too . using join query
Thanks
The error in your query is caused by the LEFT JOIN keyword that is after WHERE clause,
SELECT students_subjects.student_id,
students.house_id,
students_subjects.subject_id,
subjects.subject_name,
students.rollno,
students.first_name,
students.last_name
FROM students_subjects
LEFT JOIN students
on students_subjects.student_id=students.id
LEFT JOIN subjects
on students_subjects.subject_id=subjects.id
LEFT JOIN houses
on students.house_id=houses.id
WHERE students_subjects.class_years_section_id = 1 AND
students_subjects.school_session_id = 1 AND
students.is_active = 1
Remember that JOINs are part of the FROM Clause.
UPDATE 1
SELECT b.student_id,
a.house_id,
b.subject_id,
c.subject_name,
a.rollno,
a.first_name,
a.last_name,
d.house_name
FROM students a
INNER JOIN students_subjects b
ON b.student_id = a.id
INNER JOIN subjects c
ON b.subject_id = c.id
INNER JOIN houses d
ON a.house_id = d.id
WHERE b.class_years_section_id = 1 AND
b.school_session_id = 1 AND
a.is_active = 1
You've miss placed the WHERE clause, try this:
SELECT students_subjects.student_id,students.house_id,students_subjects.subject_id,subjects.subject_name,students.rollno,students.first_name, students.last_name
FROM students_subjects
LEFT JOIN students ON students_subjects.student_id=students.id
LEFT JOIN subjects ON students_subjects.subject_id=subjects.id
LEFT JOIN houses ON students.house_id=houses.id
WHERE students_subjects.class_years_section_id=1
AND students_subjects.school_session_id=1 AND students.is_active=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