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
I believe there is a good way to do. I searched and tried but could not find.
I want to join three tables like this (or a better way, please suggest)
SELECT listing.*, users.username, review.rNumber
FROM listing, users,review
WHERE users.uid=listing.cuid and listing.lid=review.lID
But I don't want the review.rNumber I want to get sum of it like sum(rNumber). Because one listing can have many reviews.. Thank you..
Here is what I want to achieve with this query
All the values from listing table.
only username from users table
sum of reviews from review table
The relation is
users.uid=listing.cuid and listing.lid=review.lID
or
users.uid=listing.cuid=review.sellerID
Please let me know if I need to add table..
Thanks :)
you can get the sum for user with proper group by but for join with all the listing result you could use a dinamic join
SELECT listing.*, t.username, t.sum_rNumber
fFROM listing
INNER JOIN users ON users.uid=listing.cuid
INNER JOIN review ON listing.lid=review.lID
INNER JOIN (
SELECT users.username as username, sum( review.rNumber) sum_rNumber
FROM listing
INNER JOIN users ON users.uid=listing.cuid
INNER JOIN review ON listing.lid=review.lID
GROUP BY users.username
) t on users.username = t.username
(and is more readable the explicit join sintax)
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
I have two tables: 'posts' and 'users' every post has a 'ref_id' column to get the user id who posted it.
Now, I am getting posts this way:
$this->db->query("SELECT * FROM posts WHERE time > '$timeLimit' LIMIT 50");
I can't understand how to join every result to get the poster related data as well. What I am doing right now is basically a loop inside a loop, where foreach of the result, get their user info. But it is pretty obvious that this is very wrong,
Apparently I need to start using joins, but how does one do it? this should be a really simple example to work with, I suppose.
Any help? Thank you.
SELECT posts.*, users.*
FROM posts
INNER JOIN users
ON posts.posted_by = users.id;
Like this:
SELECT
posts.*,,
users.Username
FROM posts
INNER JOIN users ON posts.ref_id = users.user_id;
Explanation:
To JOIN to any tables with each others, there are two things; the JOIN type and the join condition. There are three main types of join:
INNER JOIN, only the rows that match the join condition will be returned from the two tables no more rows. But:
LEFT OUTER JOIN, when you join two tables you will have one on the left of the join keyword and the other one will be in the right:
FROM Table1 <------------- This is the left table.
LEFT OUTER JOIN table2 .... <------------- This is the right table.
In LEFT OUTER JOIN the unmatched rows from the left table will be included in the result set.
RIGHT OUTER JOIN the unmatched rows from the right table will be included in the result set.
CROSS JOIN this will perform a Cartesian product from the two tables.
In our query, the query will reutrn all the users from the users table only if the ref_id equal to the user_id column form the posts table.
For more information and explanations:
A Visual Explanation of SQL Joins.
Another Visual Representation of SQL Joins.
Join syntax in MySQL
SELECT user.name
FROM users
INNER JOIN posts
ON posts.ref_id == user.id
AND posts.time > 50
http://www.w3schools.com/sql/sql_join_inner.asp