Sql query correction one to many tables - mysql

I have two tables one is users and second is user_education.One users can have more than one education listing so i want to get the latest user education listing
users
===============
1-id
2-email
member_experience
==============
1-id
2-user_id
3-designation
user id 1 has 4 enteries in user_education so i want to get the last record enter designation of the user
original full query is like this
SELECT u.id,u.name,u.gender,u.email,file_managed.file_name,file_managed.file_path
from users as u
INNER JOIN member_experience on (SELECT uid FROM member_experience where member_experience.uid=u.id ORDER BY id DESC LIMIT 1)=u.id
LEFT JOIN file_managed on file_managed.id= u.fid
where u.user_type ='individual' AND u.gender='male'
"INNER JOIN member_experience on (SELECT uid FROM member_experience where member_experience.uid=u.id ORDER BY id DESC LIMIT 1)=u.id "
this portion has problem as users has many record in member_experience table but i want to get only one which is latest.
thanks

Devolve the acquisition of the last record to the where statement.
drop table if exists member_experience;
create table member_experience(id int auto_increment primary key, userid int);
insert into member_experience (userid) values
(1),(2),(1);
select * from member_experience
SELECT u.id,m.id
from users as u
join member_experience m on m.userid = u.id
where m.id = (SELECT max(m.id) FROM member_experience m where m.userid = u.id)
order by u.id
Or if you want to include those with no experience
SELECT u.id,m.id
from users as u
left join member_experience m on m.userid = u.id
where (m.id = (SELECT max(m.id) FROM member_experience m where m.userid = u.id)
or m.id is null)
and u.id < 4
order by u.id

Related

MySQL alias a query for use in another query

My title isn't great, but let me explain my situation. I have a jobs table. The jobs table has 2 foreign keys to the users table: sales_rep_id and account_manager_id.
Then I have another table called contact_info with a one to one relationship to the users table.
jobs
-----
sales_rep_id
account_manager_id
...
users
-----
first_name
last_name
contact_info
-----
user_id
home_phone
If I want to do a query where I get the phone number for both people on every job I would do the following:
SELECT reps.home_phone as reps_home, account_managers.home_phone as a_m_home FROM jobs
JOIN
(SELECT * FROM users
JOIN contact_info
ON users.id = contact_info.user_id) reps
ON reps.id = jobs.sales_rep_id
JOIN
(SELECT * FROM users
JOIN contact_info
ON users.id = contact_info.user_id) account_managers
ON account_managers.id = jobs.account_manager_id
Is there anything I can do to create a temporary table with the joined data? What is the most efficient way to do this join? For example, what if I had 10 foreign keys in the jobs table to the users table, and I needed the phone_number for all 10?
This should be obtain all the info you need
SELECT j.*
, u1.first_name as sales_rep_first_name
, u1.last_name as sales_reps_last_name
, u2.first_name as manager_first_name
, u2.u1.last_name as manager_last_name
, c1.home_phone as sales_rep_home_phone
, c2.home_phone as manager_home_phone
FROM jobs as j
INNER JOIN contact_info as c1 ON j.sales_rep_id = c1.user_id
INNER JOIN user u1 ON u1.id= c1.user_id
INNER JOIN contact_info as c2 ON j.saccount_manager_id = c2.user_id
INNER JOIN user u2 ON u2.id= c2.user_id;
Define a view that joins user and contact_info.
CREATE VIEW user_contact_info
SELECT u.id, u.first_name, u.last_name, c.home_phone
FROM user AS u
JOIN contact_info AS c ON u.id = c.user_id
Then you can use this as if it's a table.
SELECT reps.home_phone as reps_home, account_managers.home_phone as a_m_home
FROM jobs
JOIN user_contact_info AS reps ON reps.id = jobs.sales_rep_id
JOIN user_contact_info AS account_managers ON account_managers.id = jobs.account_manager_id

MySql max value on column fetching the entire row

Table 'teams_members'
Team_id
User_id
Table 'users_playeraccounts'
User_id
Rank_solo
Summoner_name
Table 'users'
Id
Username
My target : Fetching the current team members with their summonername and rank_solo where rank_solo is the highest out of all their playeraccounts
So multiple entries in 'users_playeraccounts' can be belong to a single user.
Select user_id,summoner_name,rank_solo
from teams_members t
join users_playeraccounts p on t.user_id = p.user_id
join users u on u.id = p.user_id
where team_id = 103
With something added I have no clue of
try this
select a.username, u.rank_solo, u.summoner_name
from teams_members as t
inner join users_playeraccounts as u on t.user_id = u.user_id
inner join users as a on a.id in (select u.user_id from users_playeraccounts
group by u.summoner_name,
having max(u.rank_solo) );

Chat application database

I have 3 tables
users ( user_id , name )
thread ( thread_id , date_time)
subscribers ( subscriber_id , thread_id)
subscriber id is a foreign key from users table how can i get thread of 2 specefic users , i'm doing something like this but it's giving me blank rows
SELECT t.thread_id, s.subscriber_id, u.user_id
FROM subscribers s
LEFT JOIN thread t ON t.thread_id = s.thread_id
LEFT JOIN user u ON s.subscriber_id = u.user_id
WHERE s.subscriber_id = 1 AND s.subscriber_id = 2
You should check your query as #Tim Biegeleisen said.
I will highlight the portion of your query which is causing the empty set result:
WHERE s.subscriber_id = 1 AND s.subscriber_id =2
^ really?
You are asking MySQL to return records where the subscriber_id is both 1 and 2. This doesn't make any sense. In the corrected query below I have changed the WHERE condition to use OR instead of AND:
SELECT u.user_id, u.name, t.thread_id
FROM users u INNER JOIN susbscribers s ON u.user_id = s.subscriber_id
INNER JOIN thread t ON s.thread_id = t.thread_id
WHERE u.user_id = 1 OR u.user_id = 2

chat application friend list

I have 3 MySQL tables namely chat_comments, chat_friends and user_details and I want to display a friend list.
My tables:
chat_comments(comment_id,comment,user_id,user_id2,date_added)
chat_friends(user_id,user_id2,approved)
user_details(user_id, mainimage_id, fullname)
To do this, I need a query that will return the needed fields (u.mainimage_id, u.fullname, b.comment, b.user_id) so I can loop through the list to display a table.
SQL so far (help from #Andriy M):
SELECT
cc.comment,
cc.date_added,
u.fullname,
u.mainimage_id
FROM
user_details u
LEFT JOIN
chat_comments cc
INNER JOIN (
SELECT
user_id,
MAX(comment_id) AS maxcomment
FROM chat_comments WHERE user_id=2020 OR user_id2=2020
GROUP BY user_id
) a ON a.user_id = cc.user_id
AND a.maxcomment = cc.comment_id
ON a.user_id = u.user_id
WHERE u.user_id IN (
SELECT user_id2
FROM chat_friends
WHERE user_id = 2020
AND approved = 1
)
The above query returns the last comment made by the logged-in user's friends in conversation not the last comment between the logged-in user and his/her friend regardless of who made it.
I would like it to return the last comment between the logged-in user and their friend individually regardless of who made it. In the chat_messages table, user_id is the sender and user_id2 is the receiver. Hope it makes sense?
Like #imm said in a comment, you need to use an outer join. In case of a left join, the user_details table should become the left side of the join, the right side being the result of your inner join of chat_comments with your a derived table. You'll also need to remove the user_id IN (…) condition from inside the a subselect and re-apply it to the user_details table. Here:
SELECT
cc.comment,
cc.date_added,
u.fullname,
u.mainimage_id
FROM
user_details u
LEFT JOIN
chat_comments cc
INNER JOIN (
SELECT
user_id,
MAX(comment_id) AS maxcomment
FROM chat_comments
GROUP BY user_id
) a ON a.user_id = cc.user_id
AND a.maxcomment = cc.comment_id
ON a.user_id = u.user_id
WHERE u.user_id IN (
SELECT user_id2
FROM chat_friends
WHERE user_id = 2020
AND approved = 1
)
;
Alternatively, you could use a right join. In this case you would just need to move the user_id IN (…) condition, similarly to the LEFT JOIN solution above, and replace the second INNER JOIN with RIGHT JOIN:
SELECT
cc.comment, cc.date_added, u.fullname, u.mainimage_id
FROM
(
SELECT user_id, MAX(comment_id) AS maxcomment
FROM chat_comments
GROUP BY user_id
) a
INNER JOIN
chat_comments cc ON
a.user_id = cc.user_id AND
a.maxcomment = cc.comment_id
RIGHT JOIN
user_details u ON
a.user_id = u.user_id
WHERE u.user_id IN (select user_id2 from chat_friends where user_id=2020 AND approved=1)

Friend Table with mysql

I am working the friend table with mysql. I want to get friend userid and username form current username.
friend
=======
- id
- uid
- fid
Sample Data
===========
id uid fid
1 1 2
2 3 1
user
====
- id
- username
Sample Data
===========
id username
1 saturngod
2 snow
3 John
My current code is
SELECT `user`.id,`user`.username FROM friend
INNER JOIN User
ON user.id = friend.uid
WHERE friend.fid = ( SELECT `id` FROM `User` WHERE `username`='saturngod')
UNION
SELECT `user`.id,`user`.username FROM friend
INNER JOIN User
ON user.id = friend.fid
WHERE friend.uid = ( SELECT `id` FROM `User` WHERE `username`='saturngod')
It's working. I got the friend list. However, I feel , the sql is so long.
Can I reduce this sql and can we write without UNION in sql ?
You have already joined the tables, so the subselect is not needed.
SELECT u.id,u.username
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
WHERE u.username='saturngod'
UNION ALL
SELECT u2.id,u2.username
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
INNER JOIN user u2 ON (u2.id = f.fid)
WHERE u.username='saturngod'
Or you can do:
SELECT
u.id as user_id
,u.username
,u2.id as friend_id
,u2.username as friendname
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
LEFT JOIN user u2 ON (u2.id = f.fid)
WHERE 'saturngod' = u.username
If you want one row per user you can do:
SELECT
u.id as user_id
,u.username
,GROUP_CONCAT(u2.id) AS friend_ids
,GROUP_CONCAT(u2.username) as friendnames
FROM user u
INNER JOIN friend f ON (u.id = f.uid)
LEFT JOIN user u2 ON (u2.id = f.fid)
WHERE 'saturngod' = u.username <<-- optional
GROUP BY u.id
SELECT CASE WHEN user_id="$id" THEN friend_id ELSE user_id END AS friendID
FROM user_friend WHERE user_id="$id" OR friend_id="$id" order by friendID ASC