I have schema like this in my database:
I want to display all recipes (with specific fields) which specific user (given with unique_id) has added to his favourites which are desscribed in table favourite (2 fields - user id and liked recipe). I have no idea how to to that.
f.e. If user likes 5 recipes, that information is included in the favourite table (his id and recipe he liked id). I want to display fields:
recipe.unique_id,
recipe.title,
recipe.img_tumbnail_link,
recipe.add_date,
recipe.kitchen_type,
recipe.meal_type,
user.name,
user.surname,
COUNT(like.recipe_unique_id_fk) AS like_count
I've tried to do some query with this:
SELECT recipe.`unique_id`, recipe.`title`, recipe.`img_tumbnail_link`, recipe.`add_date`, recipe.`kitchen_type`, recipe.`meal_type`, user.`name`, user.`surname`, COUNT(`like`.`recipe_unique_id_fk`) AS like_count
FROM `recipe`
JOIN `favourite` ON (recipe.`unique_id` = `favourite`.`recipe_unique_id_fk`)
JOIN `like` ON (recipe.`unique_id` = `like`.`recipe_unique_id_fk`)
JOIN `user` ON (recipe.`user_unique_id_fk` = user.`unique_id`)
WHERE favourite.`user_unique_id_fk` = '565096811c0b51.08471830'
ORDER BY recipe.`add_date` DESC
From table like this:
with this query i receive only 1 row instead of 3 I should get for user with id: 565096811c0b51.08471830. Any help? Thank you.
I've added recipes table and result:)
Here is my query result:
Here is all records (no duplicate): http://postimg.org/image/ejjemnozb/
You are using join with like table as well which is having only one row thats why only one row is returned. You can calculate likes with sub-query. I have mentioned correct query below:
SELECT recipe.`unique_id`, recipe.`title`, recipe.`img_tumbnail_link`, recipe.`add_date`, recipe.`kitchen_type`, recipe.`meal_type`, user.`name`, user.`surname`,
(SELECT count(*) from `like` WHERE recipe.`unique_id` = `like`.`recipe_unique_id_fk`) AS like_count
FROM `recipe`
JOIN `favourite` ON (recipe.`unique_id` = `favourite`.`recipe_unique_id_fk`)
JOIN `user` ON (recipe.`user_unique_id_fk` = user.`unique_id`)
WHERE favourite.`user_unique_id_fk` = '565096811c0b51.08471830'
ORDER BY recipe.`add_date` DESC
Related
I'm trying to create a query with multiple joins, but it's not working for me.
I have the follow query statement:
SELECT DISTINCT s.per_id
, s.per_key
, s.per_disabled
, p.acc_id
, a.acc_name
, p.approved_acc_id
, p.per_time
FROM acc_permissions p
JOIN svr_permissions s
JOIN acc_general a
JOIN svr_group_permissions g
WHERE a.acc_id = p.acc_id
AND p.per_id = s.per_id
OR a.acc_per_group = g.group_id
AND a.acc_id = p.acc_id
Now if you don't want to examine this query, I understand so I will example my table structure;
First Table (includes users):
acc_general
Second table (includes permissions (linked to users)):
acc_permission
- This table includes rows that are linked to the acc_id in acc_general.
Multiple rows are possible for one unique acc_id in this table.
Third table (includes permissions (liked to groups)):
group_permissions
Now this includes rows that are linked to groups, each group has multiple rows in this table.
Inside acc_general there is a field called; acc_group_id, this is liked with the group_id inside group_permissions
So I need a query that returns all permissions from all players.
But it should not create duplicated permissions for a account.
So if I have an account that has a permission id 1 inside acc_permission and it has permission id 1 inside group_permissions it should ignore it.
It's hard to example, but i hope someone understands what I want.
Regards, Roel
join syntax for your query
SELECT DISTINCT s.per_id AS per_id,
s.per_key AS per_key,
s.per_disabled AS per_disabled,
p.acc_id AS acc_id,
a.acc_name as acc_name,
p.approved_acc_id AS approved_acc_id,
p.per_time AS per_time
from acc_permissions p
join svr_permissions s
ON p.per_id = s.per_id
join acc_general a
ON a.acc_id= p.acc_id
left join svr_group_permissions g
ON a.acc_per_group = g.group_id
This is one where I'm not sure if there is a better approach or not. maybe I just have a syntax error that I'm not seeing. either way here's the issue.
I am trying to get the number of my "friends" (defined by me following them and them following me ,join 1 creating a derived table) that are also following the object I'm looking at in the database. (join 2). this is in a custom function not a stored procedure so I can reuse it in several places. the only pertinant columns are profile_id and follower_profile_id. profile_id is the profile being followed, follower_profile_id is the one doing the following.
here is the query:
set followsThatCorrelate =
(
Select count(id)
from
(
select * from followers as shouldFollow
join
(
(select *
from followers
where followers.profile_id = authUserId
) as followingMe
join
(select *
from followers
where followers.follower_profile_id = authUserId
) as myFollows
on myFollows.profile_id = followingMe.follower_profile_id
) as friends1
on shouldFollow.follower_profile_id = friends1.follower_profile_id
where shouldFollow.profile_id = otherId
)
);
it says my syntax error is after I alias the derived table friends. no matter what I put after that it always throws the exception.
CREATE TABLE followers (
profile_id NVARCHAR(5),
follower_profile_id NVARCHAR(5)
);
INSERT INTO followers
VALUES ('Tom','Dick'),('Dick','Tom'),('Carry', 'Tom'),('Tom','Carry'),('John','Tom');
SET #authUserID = 'Tom';
SELECT followsme.follower_profile_id as followerID
FROM followers followsme
-- Join back to the followers table
INNER JOIN followers ifollow
-- get all of your followers followers
ON followsme.follower_profile_id = ifollow.profile_id
-- filter your followers followers to only you.
AND ifollow.follower_profile_id = #authUserId
-- this restricts the original set to only your followers
WHERE followsme.profile_id = #authUserId
I need a little help with an SQL Query. I want to count number of entries in 2 two tables. my first table called tracking_adspace_views got entry from every view of an Adspace I make. At the end of the day I want to count how much entries I have last day. The second table tracking_clicks count the clicks on a special adspace. So not every adspace which get viewed has clicks. So at the end I want something like
Adspace Id 1 has 500 Views and 3 Clicks
Adspace Id 2 hast 340 Views and 0 Clicks
and so on...
My tables looks like this
tracking_adspace_views
tracking_clicks
My query looks like this:
SELECT '2013-10-31',
views.tracking_adspace,
COUNT(views.tracking_adspace) AS tracking_adspace_views,
COUNT(clicks.tracking_adspace) AS tracking_adspace_clicks
FROM views
LEFT JOIN (
SELECT tracking_adspace,
COUNT(sub_c.tracking_adspace) AS tracking_adspace_clicks
FROM tracking_clicks as sub_c
GROUP BY sub_c.tracking_adspace
) AS clicks
ON clicks.tracking_adspace = views.tracking_adspace
WHERE views.tracking_time LIKE '2013-10-31%'
GROUP BY views.tracking_adspace
That counts the views right and where clicks are 0 are also counted correct. But if there are clicks in click column I get the value from views. Thanks for your help:)
Regards
You appear to be doing a count of a values from rows that will be duplicated by the join.
Try something like this:-
SELECT '2013-10-31' AS TrackingDate,
views.tracking_adspace,
COUNT(views.tracking_adspace) AS tracking_adspace_views,
clicks.tracking_adspace_clicks
FROM views
LEFT JOIN
(
SELECT tracking_adspace,
DATE(tracking_time) AS TrackingDate,
COUNT(tracking_adspace) AS tracking_adspace_clicks
FROM tracking_clicks
GROUP BY tracking_adspace
) AS clicks
ON clicks.tracking_adspace = views.tracking_adspace
AND DATE(views.tracking_time) = clicks.TrackingDate
WHERE DATE(views.tracking_time) = '2013-10-31'
GROUP BY TrackingDate, views.tracking_adspace, clicks.tracking_adspace_clicks
I would expect you to have a third table named tracking_adspaces or something, with a primary key called tracking_adspace. If you do, you could do this:
select '2013-10-13' as TrackingDate,
A.tracking_adspace
V.views,
C.clicks
from tracking_adspaces A
left join (
select tracking_adspace, count(tracking_id) as views
from views
where DATE(tracking_time) = '2013-10-31'
group by tracking_adspace
) V on A.tracking_adspace = V.tracking_adspace
left join (
select tracking_adspace, count(tracking_id) as clicks
from tracking_clicks
where DATE(tracking_time) = '2013-10-31'
group by tracking_adspace
) C on A.tracking_adspace = C.tracking_adspace
If not, you could use
select '2013-10-13' as TrackingDate,
distinct(A.tracking_adspace)
V.views,
C.clicks
from views A
left join (
(etc)
i am trying to write the Query for three things .My table structure is like that
You can see Schema at http://sqlfiddle.com/#!2/56c2d/1
I am trying to write the query in MYSQL
user:- table
user_id
user_fname
This is User tabke which will save User Information
group:- "group" and "subgroup" is maintain in same table using column "group_parent_group_id"
group_id
group_title
group_parent_group_id(INT)
This is group table which will save Group and Subgroups
user_group: table
user_group_id
user_group_user_id
user_group_group_id
This ill store both User and Group relation using their Id
I am trying to write the Query for three things. Fetching Users Groups, Subgroups
1) Query to fetch list of All Groups for User Register. Query is gelow and is giving error
Query:
select user.id, user.user_fname, group.group_id, group.group_title
from `user`
inner join user_group on user_group.user_group_user_id = user.user_id
inner join group on group.group_id = user_group.user_group_group_id
where user_group.user_group_user_id = 1 and user_group.group_parent_group_id = 0
2) I am Looking the query to fetch all subgroups(For Whom user is already Register) for Group Id 1,2 or 1
3) I am Looking the query to fetch all subgroups(For Whom user is Not Register yet) for Group Id 1,2 or 1. Ideal is for giving him randomly suggest a subgroup to add
Please Help. I am a newbie in DB :(
Your query is probably failing as you have a table called group, which is a reserved word. You can use back tics to delimit the name to get away with this (as follows) but it would be a better idea to change the table name.
SELECT user.id, user.user_fname, `group`.group_id, `group`.group_title
FROM `user`
INNER JOIN user_group ON user_group.user_group_user_id = user.user_id
INNER JOIN `group` ON `group`.group_id = user_group.user_group_group_id
WHERE user_group.user_group_user_id = 1
AND user_group.group_parent_group_id = 0
EDIT updated for queries I think the OP requires.
First query will get a list of all the groups (ones that have no parent group id) that a user (in this case id 28) is a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
INNER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id
INNER JOIN y2m_group ON y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id = 0
This query will get a list of all the sub groups (ones where the parent group id is greater than 0) that a user (in this case id 28) is a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
INNER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id
INNER JOIN y2m_group ON y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id > 0
This query will get a list of all the sub groups (ones where the parent group id is greater than 0) that a user (in this case id 28) is NOT a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
CROSS JOIN y2m_group
LEFT OUTER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id AND y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id > 0
AND y2m_user_group.user_group_id IS NULL
Please excuse any typos as not tested (with your test data there are no sub groups).
I have 1 table with tasks named opentask:columns: id,title,description,expires,creator_id,creator_name, executer_id, executer_name, priority_id, status_id1 table with users named user:
with columns: user_id, username
What I want is to create a query where there will be all columns from opentask table where the executer_id will be equal to the user_id of user table AND the creator_id will be equal to the user_id again. This creates a confusion because the first equality excludes the second.So I need somehow to create a query where I will include the usernames for the executer with something like where "opentask.executer_id=user_user_id" and at the same time I will include the username again (as a differend name?) for the creator with something like "where opentask.executer_id=user_user_id"So I try this, which of course I know that is missing something, can you help?
SELECT DISTINCT id, title, description, expires, creator_id, executer_id, oc_opentask.priority_id, oc_opentask.status_id, priority_name, status_name, user_id, username, (SELECT username FROM oc_opentask, oc_user WHERE oc_opentask.creator_id=oc_user.user_id) AS username2 FROM oc_opentask, oc_opentask_priority, oc_user, oc_opentask_status WHERE oc_opentask.priority_id=oc_opentask_priority.priority_id AND oc_opentask.executer_id=oc_user.user_id AND oc_opentask.status_id=oc_opentask_status.status_id ORDER BY oc_opentask.expires DESC
ReJOIN the table oc_user twice with different aliases creators and executers like so:
SELECT DISTINCT
t.*,
creators.user_name 'Creator name',
executers.username 'Executor name',
tp.priority_name,
s.status_name
FROM oc_opentask t
INNER JOIN oc_opentask_priority tp ON t.priority_id = tp.priority_id
INNER JOIN oc_user executers ON t.executer_id = executes.user_id
INNER JOIN oc_user creators ON t.creator_id = creators.user_id
INNER JOIN oc_opentask_status s ON t.status_id = s.status_id
ORDER BY t.expires DESC