WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here!

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2004, 07:04 AM
4u 4u is offline
WebProWorld New Member
 

Join Date: Nov 2004
Posts: 3
4u RepRank 0
Default mysql question

Hi! I am new bird in mysql,anybody help.
I have three table.there are person,song,likes.
Table person is described by a PersonID,LastName and FirstName. Table song include songid,songtitle,artistname and recordconame. Table likes show which person likes which song.
the question is:
1.List all persons who like the name song as personId 2?
2.Find the most popular songId.

(write SQL Select statement)
Reply With Quote
  #2 (permalink)  
Old 11-07-2004, 12:18 PM
mushroom's Avatar
WebProWorld Veteran
 

Join Date: Feb 2004
Location: Queen Charlotte B. C. Canada
Posts: 351
mushroom RepRank 0
Default

Mysql has many programing langage interfaces, you failed the mention which one you are using.
But you will need to use "count(distinct field)" as part of your query (#2),
__________________
Irony: That for most people the most "trusted" web site on the planet is for a company the has been convicted of criminal activity.

Both Security and SuSe start with "S". www.oldslides.com
Reply With Quote
  #3 (permalink)  
Old 11-16-2004, 10:55 PM
godzilla's Avatar
WebProWorld Member
 

Join Date: Aug 2003
Location: Montreal, CA
Posts: 68
godzilla RepRank 0
Default Re: mysql question

Quote:
Originally Posted by 4u
Hi! I am new bird in mysql,anybody help.
I have three table.there are person,song,likes.
Table person is described by a PersonID,LastName and FirstName. Table song include songid,songtitle,artistname and recordconame. Table likes show which person likes which song.
the question is:
1.List all persons who like the name song as personId 2?
2.Find the most popular songId.

(write SQL Select statement)
1)Do you want to get person IDs for a given song?
if yer then it is simple:
SELECT personId
FROM person NATURAL JOIN likes NATURAL JOIN song
WHERE songId='sometitle'

2)SELECT songId FROM(
SELECT songId, Count(personID) as popularity
FROM song NATURAL JOIN likes
GROUP by songId)
WHERE Popularity = MAX(popularity)

if previous does not work a simpler but less elegant answer would be:
SELECT songId, Count(personID) as popularity
FROM song NATURAL JOIN likes
GROUP by songId
ORDER by popularity DESC LIMIT 1

or even simpler
SELECT songId, Count(personID) as popularity
FROM song NATURAL JOIN likes
GROUP by songId
ORDER by popularity DESC

and the first record is what you want


In general note that if the version of MySQL that you have will complain about NATURAL word just use INNER
__________________
Everything has its reason.
Your greatest glory doesn't consist in falling, but rising every time you fall and this is what defines a perfect human.
Godzilla's pass-time
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Tags: ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Search Engine Optimization by vBSEO 3.2.0