I have three related tables users, groups and group_members. The users table contains users information and groups table contains the group name and group owner id (ie, users_id). Table group_members contains the members of each groups (user_id and group_id). I tried to load the users details of each group but the result is not correct. My tables are,
users
id firstname lastname email
1 anu mm anu#gmail.com
2 manu mohan manu#gmail.com
3 cinu ab cinu#gmail.com
4 vinu mj vinu#gmail.com
5 vijesh cc vijesh#gmail.com
6 admin admin admin#gmail.com
groups
id user_id name
1 1 group1
2 2 group2
3 2 group3
4 3 group4
group_members
id user_id group_id
1 1 1
2 2 2
3 2 3
4 3 4
5 2 3
7 1 2
8 1 3
9 3 1
But when I tried the bellow query,
select users.firstname,users.lastname,users.email,groups.name as groupname from users inner join groups on groups.user_id = users.id inner join group_members on group_members.group_id = groups.id where groups.id=3 AND users.firstname LIKE 'cin%' OR users.lastname LIKE 'cin%' OR users.email LIKE 'cin%'
getting the result (which is not expected)
firstname lastname email groupname
cinu ab cinu#gmail.com group4
In this case I used the where condition WHERE groups.id =3 but the result takes the group4 (groups.id is 4). Please suggest me a solution.
your have to use () arround your or statements:
where groups.id=3 AND (users.firstname LIKE 'cin%' OR users.lastname LIKE 'cin%' OR users.email LIKE 'cin%')
Because AND has a higher priority you query looks like:
where (groups.id=3 AND users.firstname LIKE 'cin%') OR users.lastname LIKE 'cin%' OR users.email LIKE 'cin%'
And that is not what you want.
Related
My New Project is something like a blog roll in which users can follow other users.
While listing all Posts, There will be a button to follow/unfollow under each post.
Tables used are users, posts and followers. which are as follows:
USER Table
id firstName lastName
.....................................
1 arun prasanth
2 ann antony
3 sruthy abc
6 John abc
POSTS Table
id user_id postname url
.....................................................
1 2 post1 www.url1.com
2 2 post2 www.url2.com
3 6 post3 www.url3.com
4 3 post4 www.url4.com
Followers Table
id user_id following_users_id date
..............................................
1 2 1 2018-01-25
2 2 3 2018-01-25
3 6 3 2018-01-25
4 3 6 2018-01-25
to list all posts my query was like:
SELECT * FROM posts LEFT JOIN users on posts.user_id = users.user_id;
and it worked fine as both table have corresponding rows and the result was by listing both table columns with all data.
Now My issue is while joining third table, ie followers table. problem with followers table is, it will have only one row which corresponds to current logged in users user_id and followers.user_id.
i tried all variants like LEFT RIGHT INNER and OUTER JOIN's. but no success.
the result i want is:
user.id -> xxx
user.firstName -> xxx
user.lastName -> xxx
posts.id -> xxx
posts.user_id -> xxx
posts.postname -> xxx
posts.url -> xxx
followers.id -> xxx
followers.user_id -> **Logged IN Users ID**
followers.following_users_id -> **posts.user_id**
followers.date -> xxx
the followers.id, followers.user_id, followers.following_users_id, followers.date should be printed only if followers.user_id === Logged IN Users ID AND followers.following_users_id === posts.user_id is true. else those columns should be blank.
Is there any way i can retrieve data as mentioned above of should i use something like a foreach loop?
i have 3 tables {websites} {accounts} {ads}
websites table
id title url etc
-----------------------------------
1 site1 site1.com ...
2 site2 site1.com ...
3 site3 site3.com ...
accounts table
id websiteID username etc
-----------------------------------
1 1 username1 ...
2 2 username2 ...
3 1 username1 ...
4 3 username5 ...
ads table
id accountID title etc
---------------------------------
1 1 title1 ...
2 2 title1 ...
3 1 title3 ...
5 4 title4 ...
i want to join these 3 table start from website table and get some data from website table , and accounts_count ralated to its website , and ads_count related to its account . also i want zero or null result for counts.
usernames inside account table are not unique and can be same.
titles inside ads table are not unique too and can be same.
this is my query but some times it return wrong result on counts !
SELECT
websites.id as website_id
, websites.title as website_title
, COUNT(accounts.websiteID) as accounts_count
, COUNT(ads.accountID) as ads_count
, ads.lastUpdate
, websites.activation as website_activation
FROM websites
LEFT JOIN accounts
ON websites.id = accounts.websiteID
LEFT JOIN ads
ON accounts.id = ads.accountID
GROUP BY websites.id;
can u help me :{
i want show this result in a table like this:
website_title accounts_count ads_count last update operations
-------------------------------------------------------------------------
website1 3 8 2017/07/27 etc...
website2 0 0 2017/07/27 etc...
website3 3 9 2017/07/27 etc...
website4 5 15 2017/07/27 etc...
Seems that the counts need to change.
And a MAX for the ads.lastUpdate would be more accurate.
F.e.
SELECT
websites.id as website_id
, websites.title as website_title
, COUNT(DISTINCT accounts.ID) as accounts_count
, COUNT(ads.ID) as ads_count
, MAX(ads.lastUpdate) as LastUpdateAds
, websites.activation as website_activation
FROM websites
LEFT JOIN accounts
ON websites.id = accounts.websiteID
LEFT JOIN ads
ON accounts.id = ads.accountID
GROUP BY websites.id;
The thing is when you join websites with accounts you get row for each row in accounts. Then when you join with ads you further multiplicate the row count. My guess is you now get same count for accounts_count and ads_count. Furthermore you have lastUpdate column from ads table and with agregate function.
SELECT websites.id as website_id, websites.title as website_title,
ads.lastUpdate, websites.activation as website_activation
COUNT(accounts.websiteID) as accounts_count, COUNT(ads.accountID) as ads_count,
FROM websites, accounts, ads
WHERE websites.id = accounts.websiteID
AND accounts.id = ads.accountID;
I'm stuck for hours on an issue that might be pretty simple to solve but I'm just so lost...
I got 3 tables :
user
id name
----------
1 jack
2 john
...
car
id name
----------
1 ford
2 fiat
3 alfa
4 lada
...
user_car
id_user id_car
-----------------
1 2
1 4
2 1
2 2
2 3
For example, i want to get all users with cars which have id 1 AND 2 in the user_car table so I should get the id_user 2 only and I can't find the proper way to do it.
try this untested query:
select * from user join user_car car1 on id =car1.user_id
join user_car car2 on id =car2.user_id where car1.id_car=1 and car2.id_car=2
I would use UNION for this matter:
SELECT id as id_user from user where id in(1, 2)
UNION
SELECT id as id_car from car where id in (1, 2)
You can use COUNT to do this:-
SELECT user.name
FROM user
INNER JOIN user_car ON user.id = user_car.id_user
INNER JOIN car ON user_car.id_car = car.id
WHERE car.id IN (1,2)
GROUP BY user.name
HAVING COUNT(DISTINCT car.id) = 2
Note that this could be simplified to remove the need for the car table when you are just using the id of the car.
May be you want this
SELECT *
FROM USER
WHERE id IN
(SELECT id_user
FROM user_car
WHERE id_car=1 OR id_car=2);
I have looked through some of the other posts on this site and am not seeing exactly what im looking for so here goes.
Lets say i have 2 tables
juser
-----------------------------
userID firstName lastName
-----------------------------
1 billy bob
2 jezze belle
3 bobbie sue
and:
juserrel
---------------------------------------------
id userID relUserID state
---------------------------------------------
1 1 2 approved
2 2 1 retired
3 2 1 approved
4 3 2 approved
What i am trying to do is get a result set that shows each user info about each user in the juser table and adds a column called connections to the result set which shows how many "active" connections a particular user has to another user.
the result i expect based on the tables above is
resultSet
-----------------------------------------------
userID firstName lastName connections
-----------------------------------------------
1 billy bob 2
2 jezze belle 3
3 bobbie sue 1
The query I tried is as follows
select userID, firstName, lastName , coalesce(x.cnt,0) as connections
from juser
left outer join (select count(*) cnt from juserrel where juserrel.userID =
userID or juserrel.relatedUserID = userID and juserrel.state = 'approved')
x on userID = userID
The result set i get looks like this:
resultSet
-----------------------------------------------
userID firstName lastName connections
-----------------------------------------------
1 billy bob 4
2 jezze belle 4
3 bobbie sue 4
Help Please ;)
Try:
select u.userID, u.firstName, u.lastName,
count(case when ur.state = 'approved' then 1 end)
from juser u
inner join juserrel ur on u.userID = ur.userID or u.userID = ur.relUserID
group by u.userID, u.firstName, u.lastName
SQL Fiddle Example
I've a tree structure of an association which is divided in divisions, subdivisions etc. on every level users may have memberships to certain roles.
I want to count the memberships on every "structure type" (association, division, subdivision) as defined in the table
The Table structure looks like:
table intern_structures
Contains the hierarchy (nested set, but that does not matter here)
id | intern_structure_type_id | name | parent_id | lft | rgt
1 1 My Company USA 0 1 6
2 2 Texas 1 2 5
3 3 El Paso 2 3 4
table intern_structure_types
Contains Description to the types like "association", "division", "subdivision"
id | name
1 Association
2 Division
3 Subdivision
table memberships
Contains the memberships
id | user_id | intern_structure_id | role_id
1 1 1 1
2 1 2 2
3 2 3 1
3 2 3 3
....
table roles
Contains role descriptions
id | name
1 Admin
2 Moderator
3 Clerk
I want a grouped list like:
structure_type_name | role_name | count of memberships
Association Admin 1
Association Moderator 10
Association Clerk 0 << !! I miss the zero rows!
Division Admin 7
Divison Moderator 43
Division Clerk 31
Subdivision Admin 234
Subdivision Moderator 942
Subdivision Clerk 456
What I achieved so far is this query:
SELECT
is_types.name,
roles.name,
COUNT(memberships.id)
FROM
roles,
intern_structure_types AS is_types
LEFT JOIN intern_structures AS is_elements ON is_elements.intern_structure_type_id = is_types.id
LEFT JOIN memberships ON memberships.intern_structure_id = is_elements.id
WHERE
roles.id = memberships.role_id
GROUP BY
is_types.id, roles.id
It works fine except that it doesn't list all roles because some roles don't have any memberships yet but I want them listed as well just with 0 as membership count.
I'd be very thankful for any help!
I'm assuming the counts you showed in the OP are contrived. To get the results you want, you should create a derived table of the types and roles in use and then left join that entire query to a cross join of the roles and types.
Select is_types.name
, roles.name
, Count(Z.is_type_name)
From roles
Cross Join intern_structure_types As is_types
Left Join (
Select is_types.name As is_type_name
, roles.name As role_name
From intern_structures As is_elements
Join intern_structure_types As is_types
On is_types.id = is_elements.intern_structure_type_id
Join memberships
On memberships.intern_structure_id = is_elements.id
Join roles
On roles.id = memberships.role_id
) As Z
On Z.is_type_name = is_types.name
And Z.role_name = roles.name
Group By is_types.name, roles.name