Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT from multiple tables 1

Status
Not open for further replies.

mimics

Programmer
Jun 22, 2003
13
SI
Hi!

I have 2 tables. Users and Messages. Messages contains MessageID and Username for this MessageID. Users contains Username and Data. MessageID si unique and Messages.Username is not (there can be many same Username fields in Messages but with different MessageIDs).

I would like to have a SELECT sentence which would produce a Username, Data and number of Messages (this is number of MussageIDs) for a Username which exists in Users and a Username is a Username of a MessageID. I have made this:

SELECT @Username:=Users.Username FROM Users, Messages WHERE Messages.MessageID='given_id' AND Messages.Username=Users.Username;
SELECT Users.Username, Users.Data, COUNT(Messages.MessageID) FROM Users, Messages WHERE Messages.Username=@Username AND Messages.Username=Users.Username GROUP BY Users.Username;

I think that this is an "ugly" solution. Is there a better one?


Mike
 
Code:
select u.username,udata,count(m.username)
 from users u left join messages m
   on u.username = m.username
 group by u.username,u.data
 
What about "WHERE Messages.MessageID='given_id'"?

This returns me data for all Usernames. I only need data for a Username which has given MessageID for it in Messages table.

I would like to specify a MessageID and than SQL would:
- find out Username in Messages for this MessageID (MessageIDs are unique)
- find out if this Username exists in Users table
- return data for this Username (Username, Data and number of MessageIDs for this Username in Messages table)
 
Misread.

With the current (lacking) support for SQL in Mysql it is not possible to do better than you do.
 
What should otherwise be a proper SQL sentence? And which SQL server supports it? What is that what is missing in MySQL (for what should I wait that they implement)?
 
Subselect, which is supported by almost any DBMS.

Code:
select u.username,udata,count(m.username)
 from users u join messages m
   on u.username = m.username
where u.username in
(select username from messages where messageid = 4711)
 group by u.username,u.data
 
Actually, I just realised that you don't need a subquery. It can be done with multiple joins instead.

Code:
select u.username,udata,count(m1.username)
 from messages m1 join users u
   on m1.username = u.username
   join messages m2 on
   u.username = m2.username
 where m2.messageid = 4711
 group by u.username,u.data
 
That did it! Thanks!

Is there maybe any optimization for it (maybe to specify LEFT or something)? Should I replace it with sub-select when it will be avaiable?
 
A left join will never be more efficinet than an inner join and it is not relevant in this circumstances. A subquery will probably not be better optimized either but you can compare when they are available.

If you are having performance problem you should look over your indexes. You can use the explain statement to see the query plan.
 
Thanks again. You really helped me a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top