Jump to content

[SOLVED] Organize LEFT JOIN results; SQL or PHP?


xtopolis

Recommended Posts

While trying to help another on phpfreaks, I was messing around with LEFT JOINS, and realized I didn't know how to organize the results of a query as I needed.

(Question):

How would I show the aname(Artist name) only once?

Query

SELECT a.aid, a.aname,s.sname
FROM artists a 
LEFT JOIN songs s USING (aid)

 

Table/DB Info: MySQL 5.0  (2 simple tables)

-- Table structure for table `artists`
CREATE TABLE `artists` (
  `aid` int(11) NOT NULL auto_increment,
  `aname` varchar(100) collate latin1_german2_ci NOT NULL,
  `adesc` varchar(500) collate latin1_german2_ci NOT NULL,
  PRIMARY KEY  (`aid`)
) 

-- Dumping data for table `artists`
INSERT INTO `artists` VALUES (1, 'Theme Song Heroes', 'We happily sing your theme songs.');
INSERT INTO `artists` VALUES (2, 'Ice Cream', 'Plays that ice cream truck jingle.');

-- Table structure for table `songs`
CREATE TABLE `songs` (
  `aid` int(11) NOT NULL,
  `sname` varchar(255) collate latin1_german2_ci NOT NULL
)

-- Dumping data for table `songs`
INSERT INTO `songs` VALUES (1, 'Mythbusters');
INSERT INTO `songs` VALUES (1, 'Batman');
INSERT INTO `songs` VALUES (1, 'Power Rangers');
INSERT INTO `songs` VALUES (1, 'Dirty Jobs');
INSERT INTO `songs` VALUES (2, 'We all scream');
INSERT INTO `songs` VALUES (2, 'Ice ice cream');

 

[pre]MYSQL RESULTS:

aid  aname                sname

1    Theme Song Heroes    We happily sing your theme songs.    Mythbusters

1    Theme Song Heroes    We happily sing your theme songs.    Batman

1    Theme Song Heroes    We happily sing your theme songs.    Power Rangers

1    Theme Song Heroes    We happily sing your theme songs.    Dirty Jobs

2    Ice Cream            Plays that ice cream truck jingle.    We all scream

2    Ice Cream            Plays that ice cream truck jingle.    Ice ice cream

[/pre]

 

How would I show the aname(Artist name) only once?

 

my test php code:

<?php
require 'engine/classes/mysql.php';
$db = &new MYSQL(xxxx,xxxx,xxxx,xxxx);

$sql ="SELECT a.aid, a.aname, a.adesc, s.sname
FROM artists a
LEFT JOIN songs s
USING ( aid )
LIMIT 0 , 30";

$result = $db->query($sql) or die(mysql_error());

while (list($id,$a,$d,$s) = mysql_fetch_array($result)){
  echo "Artist: $a<br />";
  echo "Desc: $d<br />";
  echo "Song: $s<br />";
  
  echo "<br />";
}
?>

Outputs all the data similar to the mysql table, with repeating the artist name, and description for each song..

Link to comment
Share on other sites

This query returns only 2 rows, instead of all..

SELECT a.aid, a.aname,s.sname
FROM artists a
LEFT JOIN songs s USING (aid)
GROUP BY a.aname

[pre]

aid  aname                sname

2    Ice Cream            We all scream

1    Theme Song Heroes    Mythbusters

[/pre]

So, it seems GROUP BY is not the solution?

Link to comment
Share on other sites

<?php
require 'engine/classes/mysql.php';
$db = &new MYSQL(xxxx,xxxx,xxxx,xxxx);

$sql ="SELECT a.aid, a.aname, a.adesc, s.sname
FROM artists a
LEFT JOIN songs s
USING ( aid )
LIMIT 0 , 30";

$result = $db->query($sql) or die(mysql_error());

while (list($id,$a,$d,$s) = mysql_fetch_array($result)){
  if( !isset( $la ) || $la != $a ) {
  	$la = $a;
  	echo "<h2>Artist: $a</h2>";
  }
  echo "Desc: $d<br />";
  echo "Song: $s<br /><br />";
}
?>

 

Might be what you want.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.