I have two tables:
posts(id,user_id,event_id}
events(event_id,name,date]}
I want to make a query, to retrieve all the names of the events for a particular user_id say id number 2.
In pseudo-code lets say
select all the event names from posts where user_id=2
try:
select events.name from posts, events
where posts.event_id = events.event_id and user_id = 2
You will need to have something slightly different depending on how you want results with no matches to display.
select name from events join posts using (event_id) where user_id = 2
Related
I have a project, where customer needs a winnner pair for events. The users of this site can "like" other user's (just like on FB), they subscribe to a particular post, and the script will generate a winner pair from the subscribers.
So I need a SQL query to randomize a winner pair from the list of pairs, where the users liked each other, and subscribed to a particular post.
How do i do that ?
I cant write a query that, because i got unexpected results.
I have 3 tables : events, likes, subs (and users ofc)
events table : event_id, event_name
subs table: sub_id, event_id, uid
likes table: liker, liked (the two uid from the users table)
Now I can make pairs from the likes table (i self-joined the table where liker = liked AND liked = liker) and randomized, but how can I join the subs and the events tables to the likes table to achieve that a randomized pair will be a subscribed users for a particular event too ?
My current query looks like this :
SELECT L.liked AS T1, L.liker AS T2
FROM likes AS L, likes AS K
WHERE L.liked = K.liker
AND L.liker = K.liked
ORDER BY rand( )
LIMIT 0 , 1
I googled everything about joins for one week, but i cant achieve that.
Say I have three tables that look like:
users:
id
event_registration:
id
user_id
event_id
events:
id
A user registers for an event and this registration is recorded in event_registration.
Is there a way I can calculate the count of events that a given set of users mutually share in a single MySQL statement? I figured out how to do this for two users, but I'd like to refactor
it to support X number of users (including just one user).
try
select count(event_id) as events
from event_registration
where user_id in (1,2,3)
group by event_id
having count(distinct user_id) = 3
You have to adjust the number 3 in the having clause acourding to the number of user_ids you are using in your in clause.
Please forgive my ignorance here. SQL is decidedly one of the biggest "gaps" in my education that I'm working on correcting, come October. Here's the scenario:
I have two tables in a DB that I need to access certain data from. One is users, and the other is conversation_log. The basic structure is outlined below:
users:
id (INT)
name (TXT)
conversation_log
userid (INT) // same value as id in users - actually the only field in this table I want to check
input (TXT)
response (TXT)
(note that I'm only listing the structure for the fields that are {or could be} relevant to the current challenge)
What I want to do is return a list of names from the users table that have at least one record in the conversation_log table. Currently, I'm doing this with two separate SQL statements, with the one that checks for records in conversation_log being called hundreds, if not thousands of times, once for each userid, just to see if records exist for that id.
Currently, the two SQL statements are as follows:
select id from users where 1; (gets the list of userid values for the next query)
select id from conversation_log where userid = $userId limit 1; (checks for existing records)
Right now I have 4,000+ users listed in the users table. I'm sure that you can imagine just how long this method takes. I know there's an easier, more efficient way to do this, but being self-taught, this is something that I have yet to learn. Any help would be greatly appreciated.
You have to do what is called a 'Join'. This, um, joins the rows of two tables together based on values they have in common.
See if this makes sense to you:
SELECT DISTINCT users.name
FROM users JOIN conversation_log ON users.id = converation_log.userid
Now JOIN by itself is an "inner join", which means that it will only return rows that both tables have in common. In other words, if a specific conversation_log.userid doesn't exist, it won't return any part of the row, user or conversation log, for that userid.
Also, +1 for having a clearly worded question : )
EDIT: I added a "DISTINCT", which means to filter out all of the duplicates. If a user appeared in more than one conversation_log row, and you didn't have DISTINCT, you would get the user's name more than once. This is because JOIN does a cartesian product, or does every possible combination of rows from each table that match your JOIN ON criteria.
Something like this:
SELECT *
FROM users
WHERE EXISTS (
SELECT *
FROM conversation_log
WHERE users.id = conversation_log.userid
)
In plain English: select every row from users, such that there is at least one row from conversation_log with the matching userid.
What you need to read is JOIN syntax.
SELECT count(*), users.name
FROM users left join conversion_log on users.id = conversation_log.userid
Group by users.name
You could add at the end if you wanted
HAVING count(*) > 0
This might not be possible but would be amazingly awesome if it were. I have the following basic table structure:
groups
group_id
other_stuff
users
user_id
other_stuff
users_to_groups
user_id
group_id
other_stuff
events
event_id
group_id (where the event belongs)
other_stuff
events is actually a set of tables for the various actions a user can perform on the site.
I would like to be able to perform a query on of the tables and have it return a result something like:
event_type event_id info_columns ...
user_join user_to_group_id
photo_upload photo_id
comment comment_id
where the value in the event_type column would be one generated by the query based on the table name of the source content.
I know I can do this using multiple queries and then piecing them together in PHP, but I was thinking that maybe there is a way to do it entirely in MySQL. Is something like this even possible? If so, what are the basic steps to make it happen?
if you have a number of select statements, and you get the data you want in each, then of course you can join them.
Others have asked similar questions, mysql-join-most-recent-matching-record-from-one-table-to-another
Now, given you're only asking for the latest event. your pseudo code goes
select <userinfo>
from users
join groups
on user=group
join (select lastest event by group
from events
) as tmp
on group=tmp
That way you do 1 query, you hand off that work to the database
I have a database of articles, which are stored in categories. For my homepage, I want to grab an article from each category (I don't care which). However, some articles are crossposted to multiple categories, so they come up twice.
I have a table called tblReview with the article fields (reviewID, headline, reviewText) and a table called tblWebsiteContent that tells the site which categories the articles are in (id, reviewID, categoryID) and finally, a table called tblCategories (categoryID, categoryName) which stores the categories.
My query basically joins these tables and uses GROUP BY tblCategory.categoryID. If I try adding 'tblReview.reviewID' into the GROUP BY statement, I end up with hundreds of articles, rather than 22 (the number of categories I have).
I have a feeling this needs a subquery but my test efforts haven't worked (not sure which query needs to contain my joins / field list / where clause etc).
Thanks!
Matt
SELECT T.categoryName, tR.headline, tR.reviewText
FROM (
SELECT tC.categoryName, MAX(tR1.reviewID) reviewID
FROM tblReview tR1 join tblWebsiteContent tWC on tR1.reviewID = tWC.reviewID
join tblCategory tC on tC.categoryID = tWC.categoryID
GROUP BY tC.categoryName) T JOIN
tblReview.tR on tR.reviewID = T.reviewID
this query will select for each category an article headline corresponding to the Max reviewId for that category (you said 'I don't care which')
Try using SELECT DISTINCT. (This will only work if your SELECT is only pulling the article ID.)
select DISTINCT reviewID