Getting columns with the same name from different Mysql tables - php

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.

Related

Counting job applied person

There are 3 tables:
1.users (id_user, name)
2.job_post (id_post,id_company,jobtitle)
3.apply_job_post(id_apply, id_post,id_user)
how to count particular job applied person - any one can help me
You need count and group them
select id_user,count(id_post) from users
left join apply_job_post on apply_job_post.id_user = users.id_user
left join job_post on apply_job_post.id_post = job_post.id_post
group by users.id_user
If you want to fetch the count of users get assigned a particular job
then use the following query:
SELECT
count(u.id_user)
FROM apply_job_post AJP
LEFT JOIN job_post JP ON JP.id_post=AJP.id_post
LEFT JOIN users U ON AJP.id_user=U.id_user
where JP.id_post=2
If you want to fetch how many jobs assigned to one particular user then you can use the following query:
The following example shows how many jobs assigned to id_user=1
SELECT
count(u.id_user)
FROM apply_job_post AJP
LEFT JOIN job_post JP ON JP.id_post=AJP.id_post
LEFT JOIN users U ON AJP.id_user=U.id_user
where u.id_user=1
If you want to fetch whole data from three tables then:
SELECT
U.*,
JP.*,
AJP.*
FROM apply_job_post AJP
LEFT JOIN job_post JP ON JP.id_post=AJP.id_post
LEFT JOIN users U ON AJP.id_user=U.id_user

Postgresql query issue

so I have a problem that I'm trying to resolve since a couple of weeks, but I'm not coming to any solution. So here are the tables that I'm trying to make a query on:
Tables
I obviously joined them (easy):
SELECT a.date,b.title,b.author,u.nick
FROM book_add a
INNER JOIN user u on(a.user_fk=u.id)
INNER JOIN book b on (a.book_fk=b.id)
INNER JOIN status s ON(a.status_fk=s.id)
WHERE s.description='active';
Now here comes the problem: I want to order the rows by date desc and distinct them, so that the last inserted row (with the newest date) the first row is. But results are very odd once they get distincted. I tried this:
SELECT a.date,b.title,b.author,u.nick
FROM book_add a
INNER JOIN user u on(a.user_fk=u.id)
INNER JOIN book b on (a.book_fk=b.id)
INNER JOIN status s ON(a.status_fk=s.id)
WHERE s.description='active' ORDER BY a.date DESC;
this works, though once i try distinctig a.book_fk results are wrong:
SELECT DISTINCT ON(a.book_fk)a.book_fk,a.date,b.title,b.author,u.nick
FROM book_add a
INNER JOIN user u on(a.user_fk=u.id)
INNER JOIN book b on (a.book_fk=b.id)
INNER JOIN status s ON(a.status_fk=s.id)
WHERE s.description='active' ORDER BY a.date DESC;
I even tried approaches like this one, but without success:
SELECT * FROM (SELECT DISTINCT
ON(a.book_fk)a.book_fk,a.date,b.title,b.author,u.nick
FROM book_add a
INNER JOIN user u on(a.user_fk=u.id)
INNER JOIN book b on (a.book_fk=b.id)
INNER JOIN status s ON(a.status_fk=s.id)
WHERE s.description='active') res ORDER BY res.date DESC
Could someone help me? I would be very happy! Thank you!
It sounds like you're trying to get one row per book, with the most recent user/status? In that case this should work:
SELECT DISTINCT ON(a.book_fk)
a.book_fk, a.date, b.title, b.author, u.nick
FROM book_add a
INNER JOIN user u on(a.user_fk=u.id)
INNER JOIN book b on (a.book_fk=b.id)
INNER JOIN status s ON(a.status_fk=s.id)
WHERE s.description='active'
ORDER BY a.book_fk, a.date DESC
;

MySQL Inner Join based query gives duplicate results

I have written a query using inner join which consist of multiple tables
SELECT *
FROM admin_info
INNER JOIN admin_login
INNER JOIN gender
INNER JOIN admin_type
INNER JOIN area
INNER JOIN document_type
INNER JOIN permissions
ON admin_login.admin_id=admin_info.admin_id
AND admin_type.admin_type_id=admin_info.admin_type_id
AND area.area_id=admin_info.area_id
AND document_type.document_id=admin_info.document_id
AND permissions.permission_id=admin_info.permission_id
The above query works but gives multiple results of the same record or duplicate records
Please help me fix the query or provide with an alternative to this query
also please suggest me ways to optimize the query for faster processing??
The above Query
SELECT *
FROM admin_info
INNER JOIN admin_login
INNER JOIN gender
INNER JOIN admin_type
INNER JOIN area
INNER JOIN document_type
INNER JOIN permissions
ON admin_login.admin_id=admin_info.admin_id
AND admin_type.admin_type_id=admin_info.admin_type_id
AND area.area_id=admin_info.area_id
AND document_type.document_id=admin_info.document_id
AND permissions.permission_id=admin_info.permission_id
Below Given is the screen shot of the results.
I want only one record per person and not duplicate records?
Apart from that when i use foreach/while loop the results vary please help?
Actually The problem was with gender table which had no reference in the query as soon as i removed the gender table inner join it worked as expected!

the whole data from tables is not coming via join query in mysql

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

Inner Joining with two tables

I need a little help setting up my query. I'm simply trying to access the amount of people who are in the same 'clan' by joining these two tables together, clan, users. Each users has a column 'clan' which is the same as the table clan's column 'roomOwner' and then I'm trying to get the table clan's information along with the amount of members so it would be like: room, roomOwner, members
So basically all I have is this:
SELECT c.*, count(u.clan) AS members FROM clans c inner join users u WHERE c.roomOwner = u.clan ORDER BY members;
It only shows one clan though. Any help please?
Your query has no GROUP BY clause. and I think it's only returning single record right? LEFT JOIN is needed here since there are possibilities that a clan has no member.
SELECT b.roomOwner, COUNT(a.clan) memberCount
FROM clan b
LEFT JOIN users a
ON a.clan = b.roomOwner
GROUP BY b.roomOwner
ORDER BY memberCount
You forgot GROUP BY. Do you have some "id" column in "clans" table? Group by that "id"
SELECT c.*, count(u.clan) AS members
FROM clans c
inner join users u ON c.roomOwner = u.clan
GROUP BY clans.id
And you need LEFT JOIN there instead of INNER JOIN if you want to see info about all clans, even having 0 users.
Perhaps this will help:
select c.*, count(links.id) as members
from clans c
left join users u on c.roomOwner = u,clan
group by u.clan
order by members

Categories