Jump to content

[SOLVED] GROUP BY problem.


Chevy

Recommended Posts

The GROUP BY I am using does not seem to be working.

 

                $trader = mysql_query("SELECT * FROM `trades` WHERE `item1`='{$searchitem['id']}' OR `item2`='{$searchitem['id']}' OR `item3`='{$searchitem['id']}' OR `item4`='{$searchitem['id']}' OR `item5`='{$searchitem['id']}' OR `item6`='{$searchitem['id']}' GROUP BY `id`");
                $tradebarray = mysql_fetch_array($trader);

                if ($tradebarray['id'] == NULL){
                    echo '';
                }
                else {
                 echo '<a href="trades.php?type='.$tradebarray['owner'].'"><b>Lot #'.$tradebarray['id'].'</b></a> owned by <a href="profile.php?user='.$tradebarray['owner'].'"><b>'.$tradebarray['owner'].'</b>
</a><br>';
                }

 

And it just displays this:

 

 

Lot #8 owned by Chevy

Lot #9 owned by Test

Lot #8 owned by Chevy

Lot #8 owned by Chevy

Lot #8 owned by Chevy

Lot #8 owned by Chevy

Lot #8 owned by Chevy

Lot #9 owned by Test

Link to comment
Share on other sites

When I echo'ed the $trader I got Resource id for about 8 itemid's...

 

This is the table structure for trades:

 

CREATE TABLE `trades` (

  `id` int(12) NOT NULL auto_increment,

  `owner` varchar(255) NOT NULL default '',

  `time` varchar(255) NOT NULL default '',

  `item1` int(12) NOT NULL default '0',

  `item2` int(12) NOT NULL default '0',

  `item3` int(12) NOT NULL default '0',

  `item4` int(12) NOT NULL default '0',

  `item5` int(12) NOT NULL default '0',

  `item6` int(12) NOT NULL default '0',

  `wanted` int(12) NOT NULL default '0',

  `description` text NOT NULL,

  PRIMARY KEY  (`id`)

)

 

Link to comment
Share on other sites

This is what it out puts 0_o it is weird...

 

SELECT `id`,`owner` FROM `trades` WHERE `item1`='15' OR `item2`='15' OR `item3`='15' OR `item4`='15' OR `item5`='15' OR `item6`='15' GROUP BY `id`

SELECT `id`,`owner` FROM `trades` WHERE `item1`='19' OR `item2`='19' OR `item3`='19' OR `item4`='19' OR `item5`='19' OR `item6`='19' GROUP BY `id`

SELECT `id`,`owner` FROM `trades` WHERE `item1`='23' OR `item2`='23' OR `item3`='23' OR `item4`='23' OR `item5`='23' OR `item6`='23' GROUP BY `id`

SELECT `id`,`owner` FROM `trades` WHERE `item1`='24' OR `item2`='24' OR `item3`='24' OR `item4`='24' OR `item5`='24' OR `item6`='24' GROUP BY `id`

SELECT `id`,`owner` FROM `trades` WHERE `item1`='25' OR `item2`='25' OR `item3`='25' OR `item4`='25' OR `item5`='25' OR `item6`='25' GROUP BY `id`

SELECT `id`,`owner` FROM `trades` WHERE `item1`='28' OR `item2`='28' OR `item3`='28' OR `item4`='28' OR `item5`='28' OR `item6`='28' GROUP BY `id`

SELECT `id`,`owner` FROM `trades` WHERE `item1`='33' OR `item2`='33' OR `item3`='33' OR `item4`='33' OR `item5`='33' OR `item6`='33' GROUP BY `id`

SELECT `id`,`owner` FROM `trades` WHERE `item1`='35' OR `item2`='35' OR `item3`='35' OR `item4`='35' OR `item5`='35' OR `item6`='35' GROUP BY `id`

Link to comment
Share on other sites

The query it is getting $searchitem['id'] is:

 

mysql_query("SELECT  `id` FROM `useritems` WHERE `itemid`='$itemiid' AND `location`='Gone'");

 

 

Table Structure for useritems:

 

CREATE TABLE `useritems` (

  `id` int(12) NOT NULL auto_increment,

  `itemid` varchar(12) NOT NULL default '',

  `owner` varchar(255) NOT NULL default '',

  `location` enum('Inventory','Safe Box','Gone') NOT NULL default 'Inventory',

  `amount` int(12) NOT NULL default '0',

  `used` int(2) NOT NULL default '0',

  PRIMARY KEY  (`id`)

)

Link to comment
Share on other sites

I know why I am not getting this...

 

The first query goes into the while loop...like so:

 

$uitem_select = mysql_query("SELECT `id` FROM `useritems` WHERE `itemid`='$itemiid' AND `location`='Gone'");

         while ($searchitem = mysql_fetch_array($uitem_select)){
                  
                $trader = mysql_query("SELECT `id`,`owner` FROM `trades` WHERE `item1`='$searchitem[id]' OR `item2`='$searchitem[id]' OR `item3`='$searchitem[id]' OR `item4`='$searchitem[id]' OR `item5`='$searchitem[id]' OR `item6`='$searchitem[id]' GROUP BY `id`") or die(mysql_error());
                $tradebarray = mysql_fetch_array($trader);

                if ($tradebarray['id'] == NULL){
                    echo '';
                }
                else {
                 echo 'ID: '.$tradebarray[id].'<br>';
                }
           
         }

Link to comment
Share on other sites

Try this (untested);

 

SELECT t.id, t.owner FROM trades AS t
INNER JOIN 
( SELECT id FROM useritems WHERE itemid='$itemiid' AND location='Gone' ) AS u
ON ( t.item1=u.id OR t.item2=u.id OR t.item3=u.id OR t.item4=u.id OR t.item4=u.id OR t.item4=u.id )
GROUP BY t.id

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.