How to join three table in MySQL database? [duplicate] - mysql

This question already has answers here:
Joining three tables using MySQL
(11 answers)
Closed 1 year ago.
I am making a food donating application. I have three tables in the database namely:
user_info
donated_info
accepted_info
The attributes of 'user_info' table are:
user_id, name, username, password, address, phone, pincode
The attributes of 'donated_info' table are:
user_id, Food_id, Food_item, No_of_serving, Expiry_date, Expiry_time, Image, status
The attributes of 'accepted_info' table are:
Food_id, user_id
I want to create a module in which the donater gets to view the status of his donations. [By default, the status attribute is set to 'to_accept'. Whenever someone accepts it, the value is changed to 'accepted'. I want to retrieve four columns: name, phone, Food_item, Image. If the Food_item is accepted, the name and phone number of the person who has accepted it will be displayed. Else those two columns will be empty. How can I write a query for this? Any kind of helps will be greatly appreciated.

You use a simple Join
SELECT name, phone, Food_item, Image
FROM accepted_info a JOIN donated_info d ON u.Food_id= d.Food_id
JOIN user_info u ON a.user_id = u.user_id
WHERE d.user_id = 1
Also try
SELECT name, phone, Food_item, Image
FROM donated_info d LEFT JOIN accepted_info a ON u.Food_id= d.Food_id
JOIN user_info u ON a.user_id = u.user_id
WHERE d.user_id = 1

Related

Joining tables in MySQL and requesting data from second table only

I'm trying to join 2 tables where I need to show only 3 columns from the second one where another column is used as a comparison.
For example:
Table one is called employee: it has a column called user_id and some other columns
Table two is called people: it has a column called user_id which included some of the employees user_ids
The columns I want to select are all from table people! (firstname, lastname, email)
I tried the following but something going wrong:
SELECT userid, firstname, lastname, email
FROM people
JOIN employee
WHERE people.userid = employee.userid;
I'm not sure what am I doing wrong, could you please help me correct it?
You can try this query:
SELECT
p.userid,
p.firstname,
p.lastname,
p.email
FROM
people as p,
employee as emp
WHERE
p.userid = emp.userid
Looking at your script, it looks like you'll run into ambiguous columns in at least your userid. You want to explicitly tell SQL where the column comes from like in your WHERE clause if there are columns sharing the same name between the two tables.
SELECT
userid, -- AMBIGUOUS
firstname,
lastname,
email
FROM people
JOIN employee
WHERE people.userid = employee.userid;
Example solution:
SELECT
people.userid,
people.firstname,
people.lastname,
people.email
FROM people
JOIN employee
WHERE people.userid = employee.userid;
For this issue you can use this query
let suppose that I have a users table where a user have zero to one profile picture
I need the user (Name,LastName,BirthDate) for users who have no profile picture
I can use this query
select *
from user c
where NOT EXISTS (
select 1
from photo p
where p.id = c.photo_id
)
in this where you can use any field between this two table
removing the not will result on the users who have a profile picture
hope this help you
you can search for SEMI JOIN and ANTI JOIN for more informations
i think this query will solve your problem
insert into table1 (clmn_1,clmn_2,clmn_3) SELECT clmn_1,clmn_2,clmn_3 FROM table2 where id = value

Joining a table in mysqli and then fetch the matched result [duplicate]

This question already has answers here:
Select all records don't meet certain conditions in a joined table
(2 answers)
Closed 5 years ago.
i am having two tables in my DB like members and payments . Table Members has the name and id of users and payment table has the id,amount and session of payments like if user 1 has paid 1500 for a session 3 then table payment has the following details. mid 1, session 3 and amount 1500.
Now i want to fetch the names of all the users which have not aid for session 1
i am using the following query but it is not working
SELECT NAME
,id
FROM member m
,payment p
WHERE (
p.session = '3'
AND m.id != p.mid
)
This is not giving me the required result please help me .
What about something like this:
SELECT NAME
,id
FROM member m
inner JOIN payment paid
ON m.id = paid.mid
AND paid.sessionid = '1'
LEFT JOIN payment p
ON m.id = p.mid
AND p.sessionid = '3'
WHERE p.id IS NULL
When you want to get data from more than 2 tables , you have to use join and if you don't want to use join as you are doing, then make sure you have exact relation between that tables, e.g a.id = b.membe_id etc..
And in your case, i think the relation is not right, make sure you have something common in two tables.
Thanku.

Mandatory disjoint: beautiful query?

I have a schema with table
user (username, password, fullname, usertype)
there are 4 types of users and for each there is a table with additional attributes for specific type:
individual(username (FOREIGN), education, work_since)
corporation*(username (FOREIGN), headquarters, office, num_employees)
and a couple more...
there could be only 1 record in all the additional tables for user.
I need to display all of the user information from user table and additional attribute table based on user type.
The first thing that came to mind was to first query user table, and then, based on type returned, query one of the related tables... but that would be too many queries, so I was wondering, is it possible to do it in a single query?
Use left join:
select u.*,
(case when i.username is not null then 'individual'
when c.username is not null then 'corporation'
end) as usertype,
i.education, i.work_since,
c.headquarters, c.office, c.num_employees
from users u left join
individual i
on i.username = u.username left join
corporation c
on c.username = u.username;

Retrieving data from 3 Mysql tables

Suppose I have 3 different tables relationships as following
1st is tbl_users(id,gender,name)
2nd is tbl_feeds(id,user_id,feed_value)
3rd is tbl_favs(id,user_id,feed_id)
where id is primary key for every table.
Now suppose I want to get data where those feeds should come which is uploaded by Gender=Male users with one field in every row that should say either the user who is calling this query marked that particular feed as favourite or not.
So final data of result should be like following :
where lets say the person who is calling this query have user_id=2 then is_favourite column should contain 1 if that user marked favourite that particular feed otherwise is_favourite should contain 0.
user_id feed_id feed_value is_favourite gender
1 2 xyz 1 M
2 3 abc 0 M
3 4 mno 0 M
I hope you getting my question , I m able to get feeds as per gender but problem is I m facing problem to get is_favourite flag as per particular user for every feed entry.
I hope some one have these problem before and I can get help from those for sure.
I would be so thankful if some one can resolve my this issue.
Thanks
Something like this should work:
SELECT
u.id AS user_id.
fe.id AS feed_id,
fe.feed_value,
IFNULL(fa.is_favourite, 0),
u.gender
FROM
tbl_users u
JOIN
tbl_feeds fe ON (fe.user_id = u.id)
LEFT JOIN
tbl_favs fa ON (
fa.user_id = u.id
AND
fa.feed_id = fe.id
)
In order to link your tables, you need to find the most common link between them all. This link is user_id. You'll want to create a relationship between all tables with JOIN in order to make sure each and every user has data.
Now I don't know if you're planning on making sure all tables have data with the user_id. But I would use INNER JOIN as it will ONLY show records of that user_id without nulls. If the other tables could POSSIBLY (Not always guaranteed) you should use a LEFT JOIN based on the tables that is it possible with.
Here is an SQLFiddle as an example. However, I recommend you name your ID fields as appropriate to your table's name so that way, there is no confusion!
To get your isFavorite I would use a subquery in order to validate and verify if the user has it selected as a favorite.
SELECT
u.userid,
u.gender,
f.feedsid,
f.feedvalue,
(
SELECT
COUNT(*)
FROM
tbl_favs a
WHERE
a.userid = u.userid AND
a.feedsid = f.feedsid
) as isFavorite
FROM
tbl_users u
INNER JOIN
tbl_feeds f
ON
u.userid = f.userid
~~~~EDIT 1~~~~
In response to your comment, I have updated the SQLFiddle and the query. I don't believe you really need a join now based on the information given. If you were to do a join you would get unexpected results since you would be trying to make a common link between two tables that you do not want. Instead you'll want to just combine the tables together and do a subquery to determine from the favs if it is a favorite of the user's.
SQLFiddle:
SELECT
u.userid,
f.feedsid,
u.name,
u.gender,
f.feedvalue,
(
SELECT
COUNT(*)
FROM
tbl_favs a
WHERE
a.userid = u.userid AND
a.feedsid = f.feedsid
) as isFavorite
FROM
tbl_users u,
tbl_feeds f
ORDER BY
u.userid,
f.feedsid

MySQL join, even when 0 [duplicate]

This question already has answers here:
What is the difference between "INNER JOIN" and "OUTER JOIN"?
(28 answers)
Closed 8 years ago.
Im doing the follwing, to create an user report
SELECT b.username, b.name, b.permissiontoedit, a.total, a.user
FROM (SELECT user, Count( * ) AS total
FROM products
GROUP BY user)a
JOIN user b ON a.user = b.username
This should give a table with the username, full name, permision (1/0) and the total of entries.
Sadly, the query does only list users, which made more 1 or more entries in the table products. But i want all users, and if the have not made any entries in products it should display 0 or nothing.
where do i made a mistake?
Using a LEFT JOIN you can get your result:
SELECT
u.username, u.name, u.permissiontoedit, COUNT(p.user) as total
FROM
user u
LEFT JOIN
products p
ON
u.username = p.user
Note: COUNT(expression) counts only NOT NULL rows in contrast to COUNT(*) that counts every row.