Get user id via mysql - mysql

I have the two tables grades and feedback and the following MySQL query:
SELECT id, mingrade, maxgrade, quizid
FROM feedback
WHERE quizid=6
ORDER BY id DESC LIMIT 6
How do I need to modify the query to add userid at the first table?

What are the fields of the grades and feedback tables? Use a JOIN that links the two based on a common attribute.
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name
WHERE condition;
So if your two tables are grades and feedback, and both have the field userid you could say:
SELECT id, userid, mingrade, maxgrade, quizid
FROM feedback
INNER JOIN grades ON feedback.userid = grades.userid
WHERE quizid = 6
ORDER BY id DESC LIMIT 6;
I think that should work.

Related

SQL: Join on multiple tables

I am trying to join the following tables:
Table 1: users (id, name)
Table 2: pivot_table (user_id, user_id2)
This table hold the relationship between the users. One user can be related to many other users.
Table 3: contacts (id, user_id, name)
I am trying to select a user and then find its related users and contacts.
I have tried to use the join, i am successfully able to get either the contacts or the users but not the both.
I used the following query:
select
contacts.id as contact_id,
users.id as user_id,
pivot.user_id2 as user2_id
from
`contacts`
inner join `users` on `contacts`.`user_id` = `users`.`id`
inner join `pivot` as `pivot` on `pivot`.`user_id` = `contacts`.`user_id`
Suppose i have the following data in the tables
Users:
id name
1 Sahil
2 Shubham
3 Xyz
Pivot table data
user_id user_id2
1 2
Contacts table data
id user_id name
1 1 Abc
2 1 XYZ
Now what is get from the query is
contact_id user_id user_id2
1 1 2
2 1 2
but what i need is
contact_id user_id user_id2
1 1 null
2 1 null
null 1 2
I am not able to figure out what wrong i am doing.
Thanks
Try this instead:
SELECT B.id as contact_id, A.id as user_id, C.user_id2 as
user2_id
FROM `users` A LEFT JOIN `contacts` B
ON A.`id`=B.`user_id` LEFT JOIN `pivot` C
ON A.`id`=C.`user_id`;
See MySQL Join Made Easy for insight on using joins.
SELECT users.id,GROUP_CONCAT(contacts.user_id,pivot.user_id SEPERATOR ",")contacts.user_id,pivot.user_id
FROM contacts INNER JOIN users ON contacts.user_id=users.id INNER JOIN pivot ON users.id=pivot.user_id;
I solved the issue by myself with the use of Union to get the desired results.
Thanks for your support.
It seems you want a query to show unrelated data:
a user's contacts
a user's related users
I'd suggest two separate queries for this. If you want to do this in one query, use UNION ALLto combine two query results:
select contact_id, user_id, null as user_id2 from contacts
union all
select null as contact_id, user_id, user_id2 from pivot_table;

Get latest user comments from posts sql

I`m new to sql.I have two joined tables, it's posts tables posts and comments, I'm using MySql.
Post :
id, UserName, Phone , product
Comments:
id , CommentText, post_id
I'm ussing join query to join them
SELECT t1.UserName , t1.Phone, t2.Comments
FROM table1 AS t1
LEFT JOIN table2 AS t2 ON (t1.id = t2.follow_id )
And now I need to display all unique users and their last comments,so they look like that
1. User1 LastComment1
2. User2 LastComment2
3. User3 LastComment3
...
I will be very grateful for the help.
i assume that column id in both tables are AUTO_INCREMENT PRIMARY KEY.
This corelated subquery wil give you the correct answer.
Query
SELECT
Post.Username
, Post.Phone
, (SELECT
Comments.CommentText
FROM
Comments
WHERE
Comments.post_id = Post.id
ORDER BY
Comments.id DESC
LIMIT 1
)
AS LastComment
FROM
Post
You might want to add a index on the Comments.post_id to improve performance off this query.

How to join a table with specific conditions

I would like to select data from a table like this (the table name is conversations_users) :
I would like to be able to retrieve a conversation ID that includes only two users. As instance, if I search a conversation specific to users 1 and 3 the conversation number 6 should be the unique result, because the conversation 5 also includes user 2.
I have tried to perform a request like
SELECT * FROM conversations_users AS table1 JOIN
conversations_users AS table2 ON
table1.conversation_ID = table2.conversationID
WHERE table1.userID = 3 AND
table2.userID = 1
But it returns both conversations 5 and 6. How can I fix that ?
Thank you in advance,
Pierre
Add the ON clause:
SELECT * FROM conversations_users AS table1 JOIN
conversations_users AS table2
ON table1.conversation_ID = table2.conversation_ID
WHERE table1.userID = 3 AND
table2.userID = 1
Update:
To get only coversations, where only 1 and 3 are involved, you can use having clause:
SELECT table1.conversation_ID FROM conversations_users AS table1 JOIN
conversations_users AS table2
ON table1.conversation_ID = table2.conversation_ID
WHERE table1.userID = 3 AND
table2.userID = 1
Group by table1.conversation_ID
having Count(*) = 2
The query you need looks like:
SELECT conversation_ID, GROUP_CONCAT(DISTINCT userID ORDER BY userID) as users
FROM conversations_users
GROUP BY conversation_ID
HAVING users = '1,3'
The GROUP BY clause groups the rows having the same conversation_ID and from each group it generates a new record that contains the conversation_ID and the distinct values of userID, in ascending order, concatenated with comma (,).
The HAVING clause keeps only those records that have '1,3' in the column users computed by the GROUP BY clause.
The query produces the output you need but it is not efficient because it reads the entire table. It could be more efficient by picking first the conversations of users 1 and 3 and then applying the above only to them.
It looks like this:
SELECT conversation_ID, GROUP_CONCAT(DISTINCT userID ORDER BY userID) as users
FROM (
SELECT *
FROM conversations_users
WHERE userID in (1, 3)
) conversations
GROUP BY conversation_ID
HAVING users = '1,3'
In order to work faster than the previous query, the conversations_users must have an index on the userID column.
If you want to restrict to those conversations which involve exactly n number of users. I think below generic query should work. Replacing 'n' as per requirement.
select *
from conversations_users
where conversation_id IN (select conversation_id
from conversations_users
group by conversation_id
having count(userid) = 2)
Thanks,
Amitabh
The inner select grabs all conversationIDs with other users than 1 or 3
the outer select (with distinct) collects all conversations wich are NOT in this subset
SELECT DISTINCT conversationID
FROM conversations_users t1
WHERE conversationID NOT IN ( SELECT conversationID
FROM conversations_users
WHERE userID NOT in (1, 3)
)
You can use join with where condition in this case.
SELECT #, userid ,conversation_ID FROM user AS table1 JOIN
conversations_users AS table2
ON user_ID = conversation_ID
WHERE table1.userID = 3 AND
table2.userID = 1
Group by conversation_ID
You can apply suitable condition by where clause instead of group by

Selecting rows with unique field values in mysql

I have these columns for table comments:
id
content
add_date
uid
school_id
Rows can have the same school_id.
I want to select the latest data according to add_date, but only 1 row per school_id (no duplicate for school_id) with limit of 10.
I've tried many codes already and its not working for me.
Any help would be appreciated.
This is what we call Greatest N per Group. You can achieved this by putting into a subquery so it can be joined against the non-grouped table (comments).
Try this:
SELECT c.*
FROM
(
SELECT school_id, MAX(add_date) maxDate
FROM comments
GROUP BY school_id
) x INNER JOIN comments c
ON x.school_id = c.school_ID AND
x.maxDate = c.add_date
ORDER BY x.maxDate desc
LIMIT 10
select C.ID, C.Content, t1.MaxDate as [add_date], C.uid, t1.school_id
from (selet school_id, max(add_Date) as 'MaxDate'
from comments
group by school_id) T1
inner join comments C on T1.school_id = C.school_id and C.add_Date= T1.MaxDate
LIMIT 10
If you want to choose which 10 rows return, add an order by, or a Where clause
select c1.*
from comments c1
where add_date = (select max(add_date) from comments c2 where c2.school_id =c1.school_id)
order by add_date desc
limit 10
create indexes on comments(add_date) and comments(school_id, add_date)

Which entries in my list do not have an entry in my table?

How do I do something like this in MySQL?
(1274649,682844,872502,1016256)
INTERSECT
(SELECT id FROM `users` WHERE `id` IN (1274649,682844,872502,1016256))
Adapting from the comments:
These four numbers are the IDs that I have now. I want to know which of these IDs do not have an an entry in my table, and how many of them don't have an entry?
select t.id from (
select 1274649 as id union
select 682844 union
select 872502 union
select 1016256
) t
left join users u on u.id = t.id
where u.id is null
This returns those ids that haven't corresponding id in users table.
Added this is the answer to OP explanation in comment: Which entries in my list do not have an entry in my table?