I have 3 tables named user,fruits,fruits_like.
user table has id,name,email etc fields.
fruits table has id,user_id,name etc fields.
fruits_like table has id,fruit_id and user_id fields.
Now i want to retrieve fruit details from fruits including count for fruit by user_id.
I tried below query but in that i only receive fruits which is liked by $user_id. I need to retrieve all fruits by $user_id.
SELECT f.id, f.date_of_eating, f.healthiness, f.comment,
f.picture, u.user_name,count(l.id) as isLiked
FROM fruit f
LEFT JOIN user u ON f.user_id = u.id
LEFT JOIN fruit_like l on f.id = l.food_id
WHERE f.user_id = '$user_id' and l.user_id=''$user_id''
If i run this query i get all records that i want but i need to include like count for that fruit which i am not able to add in this query.
SELECT f.id, f.date_of_eating, f.healthiness, f.comment, f.picture,
u.user_name FROM fruit f LEFT JOIN user u ON f.user_id = u.id
where f.user_id = '$user_id'
can any one please help me get this done?
Thanks
Related
now here I have these tables, user can have more than category, and I want to show table like this
I've wrote this statement but It repeats user name with every category
SELECT
c.name
FROM
permission p
JOIN users u
ON u.id = p.user_id
JOIN category c
ON c.id = p.category_id
USER DATA TABLE // this table is what I want to get because every user have more than one category
username
email
categories
row
row
math, science
row
row
row, row, row
//those tables are what I have in the database
USERS TABLE
USER ID
username
email
5
row
row
6
row
row
CATEGORY TABLE
C ID
C NAME
8
math
9
science
PERMISSION TABLE
ID
USER ID
C ID
1
5
8
2
6
9
If you have only username use DISTINCT
SELECT DISTINCT
c.name
FROM
permission p
JOIN
users u ON u.id = p.user_id
JOIN
category c ON c.id = p.category_id
With more columns you should look for GROUP BY c.name
As user info in users table is unique so max keyword is used for optimization purpose. If same category is mapped multiple times with single user then use DISTINCT keyword inside GROUP_CONCAT functions. GROUP_CONCAT() is build in function for MySQL.
-- MySQL
SELECT MAX(u.username) username
, MAX(u.email) email
, GROUP_CONCAT(c.cname) categories
FROM users u
INNER JOIN permission p
ON u.userid = p.userid
INNER JOIN category c
ON c.cid = p.cid
GROUP BY u.UserID;
I am trying to pull both owner and editby. Both of those fields are INT. Inside a simple table, for example:
users:
user_id user_name
-----------------
2 johnny
3 mecca
doc:
owner content editby
----------------------
2 misc 3
SQL:
SELECT doc.owner, doc.content, doc.editby, users.user_name
FROM doc
LEFT JOIN
users
ON
users.user_id = doc.owner
WHERE
doc_id = $id
I can grab owner user_name, but I am not sure how to obtain editby on the same table. How do I go about pulling the different user names for different id fields multiple times?
Join the users table twice with different aliases
SELECT doc.owner, doc.content,
e.user_name as editor,
o.user_name as owner
FROM doc
LEFT JOIN users o ON o.user_id = doc.owner
LEFT JOIN users e ON e.user_id = doc.editby
WHERE doc_id = $id
My SQL is only returning one field when it should be returning one for each user.
Any idea where I'm going wrong? If you need additional information I can provide, but I'm just not sure where to go with this at the moment.
Here is my SQL:
SELECT uId, uForename, SUM(biProductPrice * biQuantity) AS uTotalSpent
FROM users
LEFT JOIN orders ON uId = ordUserId
LEFT JOIN basket ON ordUserId = bUserId
LEFT JOIN basketitems ON bId = biBasketId
WHERE ordStatus BETWEEN 4 AND 50
GROUP BY uId, uForename
any columns starting with u belong to the users table.
any columns starting with ord belong to the orders table.
any columns starting with b belong to the basket table.
any columns starting with bi belong to the basketitems table.
EDIT:
Everything now works fine except for my SUM, there are only 2 fields with an ordStatus between 4 and 50, so they are the only ones that apply, the biQuantity for one is 8 and the biProductPrice is 100, the other field has a biQuantity of 1 and a biProductPrice of 100, why is it returning a value of 400?
Group by the user and the sum will be returned for each one
SELECT users.id, users.name, SUM(biProductPrice) AS uTotalSpent
FROM users
LEFT JOIN orders ON uId = ordUserId
LEFT JOIN basket ON ordUserId = bUserId
LEFT JOIN basketitems ON bId = biBasketId
WHERE ordStatus BETWEEN 4 AND 50
group by users.uId, users.name
SELECT users.id, users.name, SUM(biProductPrice) AS uTotalSpent
FROM users
LEFT JOIN orders ON uId = ordUserId
LEFT JOIN basket ON ordUserId = bUserId
LEFT JOIN basketitems ON bId = biBasketId
WHERE ordStatus BETWEEN 4 AND 50
group by users.uId, users.name
I have a table called reviews. I get the most current user reviews like this:
SELECT b.item, b.item_id, a.review_id, a.review, c.category, u.username, c.cat_id
FROM reviews a
INNER JOIN items b
ON a.item_id = b.item_id
INNER JOIN master_cat c
ON c.cat_id = b.cat_id
INNER JOIN users AS u
ON u.user_id = a.user_id
ORDER BY a.review_id DESC;
What I want to do is slightly alter it to be more personable for users.
I have another table of user "connections". Kind of like Twitter. When a user follows someone, it gets logged in this table called profile_follow. This has three columns. id, user_id, follow_id. Simply: If I am user #1, and I "follow" user # 3 and user #5, two rows will be added in this table:
profile_follow
------------------------
id | user_id | follow_id
| 1 | 3
| 1 | 5
Here is how I want to change the query above. I want to only show newest reviews, from people you follow.
So I will need at least one more join, for table profile_follow. And I need to pass in a user_id (it's a php function), doing something like `WHERE profile_follow.user_id = '{$user_id}'. I think I will have to add a sub query on this, not use.
Can someone show me how to finish this query? I am not sure how to handle it from here? All of my attempts have been off so far.
I think I need to do something like:
Selectfollow_idwhereuser_id= (logged in user)
And then in the main query:
Select reviews only with profile_follow.follow_id = review.user_id.
I can't figure out how to make this filter work.
Always difficult without testing, but:
SELECT b.item, b.item_id, a.review_id, a.review, c.category, u.username, c.cat_id
FROM reviews a
INNER JOIN items b
ON a.item_id = b.item_id
INNER JOIN master_cat c
ON c.cat_id = b.cat_id
INNER JOIN profile_follow pf
ON pf.follow_id = a.user_id
WHERE profile_follow.user_id = '{$user_id}'
ORDER BY a.review_id DESC;
I have a table - comments. Users can post if not a member of the site but want to show their details if they are.
So if a user comments who is NOT a member I show their posts but don't link to their profile, because they don't have one.
So, in the following query I want to return the rows even if there is no join:
select wc.comment, wc.comment_by_name, wc.user_id, u.url from comments wc
join users u on wc.wag_uid = u.user_id
where id = '1237' group by wc.comment order by wc.dateadded desc
I want to return:
comment comment_by_name user_id url
------- --------------- ------- ----
hello dan 12 /dan
hey jane /jane
world jack 10 /jack
But the above does not return the data for jane as she does not have a user_id
Is there a way to return all data even if the join is null?
use LEFT JOIN instead
SELECT wc.comment, wc.comment_by_name, wc.user_id, u.url
FROM comments wc
LEFT JOIN users u
on wc.wag_uid = u.user_id
WHERE id = '1237'
GROUP BY wc.comment
ORDER BY wc.dateadded DESC
basically INNER JOIN only select records which a record from one table has atleast one match on the other table while LEFT JOIN select all rows from the left hand side table (in your case, it's comments) whether it has no match on the other table.