Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 10 votes, 3.10 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-16-2006, 05:46 PM   PM User | #1
aymanstar
New Coder

 
Join Date: Dec 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
aymanstar is an unknown quantity at this point
help joining tables with group by [was: MYsql help]

I have 3 tabels

1 for singer
2- for album
3-for songs and this tabel have field with name download that count how many download by song

i want to make states with top 10 in every section
in songs i make this code
PHP Code:
<?php
$result
=mysql_query("SELECT * FROM song order by download desc limit 10");
while(
$row=mysql_fetch_object($result)){
echo 
$row[name];
}
?>
i want to make top 10 album by count the album songs download
and the same in singer

Last edited by guelphdad; 12-16-2006 at 06:13 PM.. Reason: provide proper thread title
aymanstar is offline   Reply With Quote
Old 12-16-2006, 06:14 PM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
See the albums and artists link in my signature. that article should give you a starting point to get most of your code and we can revise it from there.
guelphdad is offline   Reply With Quote
Old 12-16-2006, 06:29 PM   PM User | #3
aymanstar
New Coder

 
Join Date: Dec 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
aymanstar is an unknown quantity at this point
thx for your replay

can you make an php example

this my data base

Code:
#
# Table structure for table `sing`
#

CREATE TABLE `sing` (
  `id` int(11) NOT NULL auto_increment,
  `Name` varchar(255) NOT NULL default '',
  `Description` text NOT NULL,
  `photo` text NOT NULL,
  `Category` varchar(255) NOT NULL default '0',
  `Site` varchar(255) NOT NULL default '',
  `fans` varchar(255) NOT NULL default '',
  `Country` varchar(255) NOT NULL default '',
  `Lyrics` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

# --------------------------------------------------------

#
# Table structure for table `song`
#

CREATE TABLE `song` (
  `id` int(10) NOT NULL auto_increment,
  `Name` varchar(255) NOT NULL default '',
  `mp3` varchar(255) NOT NULL default '',
  `rm` varchar(255) NOT NULL default '',
  `video2` varchar(255) NOT NULL default '',
  `video` varchar(255) NOT NULL default '',
  `remix` varchar(255) NOT NULL default '',
  `download` varchar(255) NOT NULL default '',
  `Category` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

# --------------------------------------------------------

#
# Table structure for table `tap`
#

CREATE TABLE `tap` (
  `id` int(10) NOT NULL auto_increment,
  `Name` varchar(255) NOT NULL default '',
  `addby` varchar(255) NOT NULL default '',
  `photo` varchar(255) NOT NULL default '',
  `Category` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
aymanstar is offline   Reply With Quote
Old 12-16-2006, 08:48 PM   PM User | #4
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
before you make a php query, get an sql query working. all that php is used for is to format the output.

show what you have attempted as far as an sql query goes.
guelphdad is offline   Reply With Quote
Old 12-17-2006, 06:36 PM   PM User | #5
aymanstar
New Coder

 
Join Date: Dec 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
aymanstar is an unknown quantity at this point
ok what the query i will write??
aymanstar is offline   Reply With Quote
Old 12-17-2006, 11:28 PM   PM User | #6
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
if you have looked at the example in the article you should have an idea how to start.

try a query, even if just to get partial data from your tables. when you do a three table join, first do a join on two of the tables. when you get that working then add the third table. once you have the basics down then you can specify all of the columns needed.
guelphdad is offline   Reply With Quote
Old 12-19-2006, 12:43 PM   PM User | #7
aymanstar
New Coder

 
Join Date: Dec 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
aymanstar is an unknown quantity at this point
thanks about your help

i done with this cos but i have proplem that the output calculate download by row not sum

for example
Bryen adam have 3 song
1 song have 3 download time
2 song 2
3 song 4
i want the total in my code when see the output
9 but the outbout is 3 below my cod


Code:
$result=mysql_query("SELECT sing.Name, count( download )  AS songtotal
FROM sing
LEFT  OUTER  JOIN tap ON sing.id = tap.Category
LEFT  OUTER  JOIN song ON song.Category = tap.id
GROUP  BY Name
ORDER  BY Name");
while($row=mysql_fetch_object($result)){
aymanstar is offline   Reply With Quote
Old 12-19-2006, 01:11 PM   PM User | #8
aymanstar
New Coder

 
Join Date: Dec 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
aymanstar is an unknown quantity at this point
and then try this code
but the proplem not sum all album and sort by singer have hightest download
i
Code:
SELECT sing.Name, sum(case when song.Category=tap.Category and tap.Category=sing.id  then download else 0 end )  AS songtotal
FROM sing
LEFT  OUTER  JOIN tap ON sing.id = tap.Category
LEFT  OUTER  JOIN song ON song.Category = tap.id
GROUP  BY Name
ORDER  BY Name

Last edited by aymanstar; 12-19-2006 at 01:32 PM..
aymanstar is offline   Reply With Quote
Old 12-20-2006, 12:40 AM   PM User | #9
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
can you show the exact columns and rows for all three tables that you are trying to get for Bryan Adams? that might clarify your problem.
guelphdad is offline   Reply With Quote
Old 12-20-2006, 04:58 AM   PM User | #10
aymanstar
New Coder

 
Join Date: Dec 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
aymanstar is an unknown quantity at this point
  1. singer table
    (sing in database)
    • id=singer id
      Name=singer name
  1. album tabel
    (tap in database)
    • id=album id
      Name = album name
      Category =singer id that same in sing table
  1. song tabel
    (song in database)
    • id=song id
      Name = song name
      Category =album id that same in tap table
      download=number of download and i want to sort the output of query 2 times frist 1 sort singer by song download 2nd one sort album by song download limit 10 desc


example
  1. singer table
    (sing in database)
    • id=4
      Name=bryen adam
  1. album tabel
    (tap in database)
    • id=2
      Name = her i am
      Category =4
  1. song tabel
    (song in database)
    • id=1
      Name = her i am
      Category =2
      download=55
    • id=2
      Name = please forgive me
      Category =2
      download=55

i need the output
TOP SINGER
1 Tamer Hosni 176 download
2 bryn adam 100 download
TOP ALBUM
1 Enaya Bet7ebak 176 download
2 her i am 100 download
Example query
PHP Code:

#
# Table structure for table `sing`
#

CREATE TABLE `sing` (
  `
idint(11NOT NULL auto_increment,
  `
Namevarchar(255NOT NULL default '',
  `
Descriptiontext NOT NULL,
  `
phototext NOT NULL,
  `
Categoryvarchar(255NOT NULL default '0',
  `
Sitevarchar(255NOT NULL default '',
  `
fansvarchar(255NOT NULL default '',
  `
Countryvarchar(255NOT NULL default '',
  `
Lyricsvarchar(255NOT NULL default '',
  
PRIMARY KEY  (`id`)
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

#
# Dumping data for table `sing`
#

INSERT INTO `singVALUES (1'Tamer Hosni''''enayabetth2.jpg''0''''''Egypt''');
INSERT INTO `singVALUES (4'Bryan Adams''''''0''''''Egypt''');

# --------------------------------------------------------

#
# Table structure for table `song`
#

CREATE TABLE `song` (
  `
idint(10NOT NULL auto_increment,
  `
Namevarchar(255NOT NULL default '',
  `
mp3varchar(255NOT NULL default '',
  `
rmvarchar(255NOT NULL default '',
  `
video2varchar(255NOT NULL default '',
  `
videovarchar(255NOT NULL default '',
  `
remixvarchar(255NOT NULL default '',
  `
downloadvarchar(255NOT NULL default '',
  `
Categoryvarchar(255NOT NULL default '',
  
PRIMARY KEY  (`id`)
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

#
# Dumping data for table `song`
#

INSERT INTO `songVALUES (1'Ray7 Balk''''''''''''176''1');
INSERT INTO `songVALUES (2'her i am''''''''''''55''2');
INSERT INTO `songVALUES (3'please forgive me''''''''''''45''2');

# --------------------------------------------------------

#
# Table structure for table `tap`
#

CREATE TABLE `tap` (
  `
idint(10NOT NULL auto_increment,
  `
Namevarchar(255NOT NULL default '',
  `
addbyvarchar(255NOT NULL default '',
  `
photovarchar(255NOT NULL default '',
  `
Categoryvarchar(255NOT NULL default '',
  
PRIMARY KEY  (`id`)
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

#
# Dumping data for table `tap`
#

INSERT INTO `tapVALUES (1'Enaya Bet7ebak''Wolfman''''1');
INSERT INTO `tapVALUES (2'Her I am''''''4'); 

Last edited by aymanstar; 12-20-2006 at 05:22 AM..
aymanstar is offline   Reply With Quote
Old 12-20-2006, 06:14 AM   PM User | #11
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
I believe you want SUM and not COUNT.
guelphdad is offline   Reply With Quote
Old 12-20-2006, 12:52 PM   PM User | #12
aymanstar
New Coder

 
Join Date: Dec 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
aymanstar is an unknown quantity at this point
i use sum allready

PHP Code:
SELECT sing.Namesum
CASE  
WHEN song.Category tap.Category AND tap.Category sing.id
THEN download
ELSE 
END  
)  AS songtotal
FROM sing
LEFT  OUTER  JOIN tap ON sing
.id tap.Category
LEFT  OUTER  JOIN song ON song
.Category tap.id
GROUP  BY Name
ORDER  BY songtotal DESC 
but the output
Name songtotal
Tamer Hosni 176
Bryan Adams 0


but i need the output will be
Name songtotal
Tamer Hosni 176
Bryan Adams 100

Last edited by aymanstar; 12-20-2006 at 01:15 PM..
aymanstar is offline   Reply With Quote
Old 12-21-2006, 12:14 PM   PM User | #13
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
please post sample data that will give you the output you are looking for. your sample data above does NOT match the output you are looking for so it is hard to tell why it isn't working.

secondly, are you trying to get your output directly in phpmyadmin (or some other GUI) or the mysql client? and NOT messing up perhaps with php or other code where your output is getting messed up?
guelphdad is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:31 PM.


Advertisement
Log in to turn off these ads.