There are three tables
posts table
id username
1 ahmed
users table
id title
1 test post
post_users
user_id post_id
1 1
post_users (have two fields user_id and post_id) many to many
when I want to send post id=1, I want to select users from users table that don't have a row in post_users table (in one query). How?
I think what you are looking for is something along these lines. INNER JOIN:
SELECT DISTINCT USERS.ID
FROM USERS
INNER JOIN POST_USERS
ON USERS.ID=POST_USERS.ID
WHERE POST_USERS.POST_ID <> your_input_value
I made a few assumptions, if this doesn't work please provide some more details.
Related
I am currently querying from 4 different tables,
users, memberships_users, membership_levels, phone_area_code
From the users table, I would like to select
id
user_login,
user_email,
display_name
The memberships_users table contains a user_id column as well as an id column. The user_id column is not unique, and there are many repeating user ids.
From the memberships_users table, I would like to select
initial_payment,
billing_amount,
cycle_period,
cycle_number,
from the row with the largest id for each user_id
and
name from the membership_levels table and
meta_value from the phone_area_code table
which should all relate the id in the users table
This is basically pseudo-code of one way of getting what you want.
First, create a derived table of each user_id and the max(id) from the membership table. This will yield all distinct user_id's aligned with the associated max id from the membership. The next step is to join the two physical tables with the derived one to get the user and the associated membership record contain the max membership id.
NOTE : If your memberships_users table is on the large side then you will get better performance dropping the max_membership_id data into a temp table and adding appropriate indexes.
;WITH max_membership_id AS
(
SELECT user_id, max_id = MAX(id)
FROM memberships_users
GROUP BY user_id
)
SELECT
u.id, u.user_login, u.user_email, u.display_name,
mu.initial_payment, mu.billing_amount, mu.cycle_period, mu.cycle_number
FROM
users u
INNER JOIN max_membership_id mmi ON mmi.user_id = u.user_id
INNER JOIN memberships_users mu ON mu.id = mmi.max_id
Who can help me?
I have 4 tables posts, users, users details and comments.
Here is the strucutre:
posts
id
title
description
users
id
username
password
users_details
id
user_id
avatar
location
comments
id
post_id
user_id
parent
comment
posted
I have a query which i want to display comments on specific post.
This is what i written but avatar column it doesn't show any results. It seems i written it wrong on join or something.
SELECT comments.id as comment_id,
comments.user_id as user_id,
comments.post_id as post_id,
comments.parent as parent,
comments.comment as comment,
comments.posted as posted,
users_details.avatar as avatar,
users.username as username
FROM `comments`
LEFT JOIN users ON users.id = comments.user_id
LEFT JOIN users_details ON users_details.user_id = users.id
WHERE comments.post_id='60337'
ORDER BY posted ASC
Here is a screenshot of the table with results https://i.imgur.com/VQ8KwV1.png
I want to mention the avatar field it has a value for that user in users_details table so it should be displayed...
Thanks!
Two potential reasons I could tell causing this issue.
1. The users do not have an avatar.
Try: Select Avatar from Users_Details where User_Id in (2,9212) and make sure it returns some data.
2. The Primary/Foreign key being used for joining the tables is not matching
Run those queries below and ensure that they are returning the same users.
select * from users where id in (2,9212)
select * from users_details where user_id in (2,2912)
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
i've some problems with a specific mysql query an an specific construct.
There are 2 tables:
table users (id, username)
table groups (id, groupname)
these 2 tables are in an m:n relation, but there are 2 tables for that.
First in maps user to groups
table usertogroups (idmaster, idslave)
where idmaster is related to users.id and idslave is related groups.id
Second maps groups to users
table groupstouser (idmaster, idslave)
where idmaster is related to groups.id an idslave is related to users.id
Depend on the application it could not be changed.
Now i want to get all groups with the depending users in one query with the relation of both table, groupstouser and usertogroups.
I've tried al lot of statements, but if I take the second table in it doesn't work.
Any helpfull Ideas?
Use this as an inline view to get the data from both association tables :
((SELECT idmaster AS userid, idslave AS groupid FROM userstogroup)
UNION
(SELECT idslave AS userid, idmaster AS groupid FROM groupstouser)) all_associations
Then you can query like this :
SELECT groups.groupname, users.username
FROM groups
INNER JOIN ((SELECT idmaster AS userid, idslave AS groupid FROM userstogroup)
UNION
(SELECT idslave AS userid, idmaster AS groupid FROM groupstouser)) all_associations
ON groups.id = all_associations.groupid
INNER JOIN users
ON users.id = all_associations.userid
And here's an SQL Fiddle.
I am not sure, it might solve your problem:
(SELECT * FROM usertogroups WHERE idmaster=10)
UNION
(SELECT * FROM groupstouser WHERE idslave=10)
I think your database design is wrong.
When a user is assigned to a group only single table can be used for it. You must be saving duplicate records in both usertogroups and groupstouser.
Try to get your data from only single table.
SELECT * FROM usertogroups order by idslave
If I am wrong that you are not saving duplicate data in both the tables, then specify reason of having two tables
I know the question seems duplicate, but I don't know how to ask it differently.
I have two very simple tables in MySQL database, The first is table Users
id, user_id
1 1
2 3
4 4
The second is table Friends
id, user_id, friend_id
1 1 3
2 1 4
3 1 8
I dumped the data from CSV file that I would like to clean. I need to check if friend_id exists in table 1 as well. The first table has around 30000 rows, but the second table has around 30 million rows.
And I use this query to check
SELECT u.user_id, uf.friend_id as exists_friend_ids
FROM Users u, Friends uf
WHERE u.user_id = '1'
and uf.friend_id IN (select user_id from eventify.Users)
However, my desired output would be this but as I cannot run the above query to actually give my test results I cannot continue.
user_id, exists_friend_ids
1 3
1 4
You can see that 8 is not there, because it doesn't exist in Users table. But as the second table has over 30 million records it's just running forever on my computer. Am I doing it right or this is the only way to do it. Or should I learn Hadoop instead?
I have updated my query to use equal join.
Have you tried a LEFT JOIN query with a GROUP BY friend_id ? If a user doesn't exist, it won't add a line to the result.
If all you are doing is cleaning the table then you have some flexibility since the fact that the query runs slow will not have a great impact since you will want to run it only once. Here are a couple of different options:
use a left join to find the rows in friends without the corresponding friend id in the users table (untested):
SELECT Friends.id, Users.user_id
FROM Friends LEFT JOIN Users on Friends.friend_id = Users.user_id
WHERE Users.user_id is NULL
Then delete the records you find
use an inner join to fin the friends that exist. Then create a new table with those records (untested)
SELECT Friends.id, Users.user_id
FROM Friends INNER JOIN Users on Friends.friend_id = Users.user_id
And insert the resulting rows into a new table which will become your new "Friends" table.
Hope that helps
I don't understand why you do the CASE construct here. If you want to get a list of all friend_ids that don't exist in the users table, then what about something like:
select friends.friend_id,
count(*)
from friends
where friends.friend_id not in (select users.user_id
from users)
group by 1
You will of course have an index on users.user_id...