Jump to content

SELECT rows WHERE values are the same?


TimDOES

Recommended Posts

What values are x and y? Will this work if the "same Values" are undefined.

 

i.e. I have selected all machines made by $brand and now need to select each group of repeating column values ( Machine Type ).

 

Thank you for taking the time to help.

Link to comment
Share on other sites

The main categories I will be using are machineMfctr, machineType, machineModel.

 

My first query selects all the products made by a certain manufacturer.

$query = "select * from ARL2008 where machineMfctr = '$brand'";

 

Now I have multiple products for some Models and would like to be able to select them so the model name is only listed once while with all products for the model listed under it. Should this be done with another query or is there a better way to do this?

Link to comment
Share on other sites

Well, you only listed one of the queries, so I'm guessing here.

 

Right now are you getting query results from one SELECT, looping through the results and running another SELECT against another table based on the results of the first query?

 

If so, let's pretend that this is your table structures:

 

table1 has columns data, manufacturer, someotherdata, and somedate.

table2 has columns manufacturer, modelname, and price

 

and let's say you're doing it this way now:

 

SELECT * FROM table1 ORDER BY manufacturer;
while (results from table equals $row){
  SELECT * FROM table2 WHERE manufacturer = {$row['manufacturer']}
  // results outputted here...
}

 

If that's the case, then you're wanting to do this:

SELECT * FROM table1, table2 WHERE table1.manufacturer = table2.manufacturer ORDER BY table1.manufacturer, table2.modelname

Link to comment
Share on other sites

No, actually there is only one table that I am pulling from. I just need to sub select the group of products that are made for a certain model of machine in order to not label the model above every product (only post model once and then post all products under it). I am trying to figure out how to do this so I don't know the second query yet.

 

Thank you for the help.

Link to comment
Share on other sites

Is this wrapped in PHP code? Because I do this kinda thing all the time, and I just use an if statement and a variable, like this:

 

<?php
// your selects 'n stuff up here...

$manuf = "";
while ($row = mysql_fetch_array($result)){
  if ($row['manufacturer'] != $manuf){
    echo "some header with {$row['manufacturer']}";
    $manuf = $row['manufacturer'];
  }
  echo "product data...";
}
?>

 

Make sense?

Link to comment
Share on other sites

Correct, they all have duplicates. Here is the code I have so far.

$query = "select machineTyoe from ARL2008 where machineMfctr = '$brand'";

$result = mysql_query($query);
if (!$result) {
    die("Query to show fields from table failed");
}
$row = mysql_fetch_array($result) or die(mysql_error());


while($row = mysql_fetch_array($result)){
echo "<h3>".$row['MachineModel']."</h3>";
echo "<div class='threeCol emptyBlox'>".$row['Brand']."</div><a class='blox' style='padding:0px;' href='toner/".$Partno.".htm'>
        <div class='threeCol'>".$row['DESCR1']."</div></a><a class='blox' style='padding:0px;' href='toner/".$Partno.".htm'>
        <div class='threeCol'>$".$row['Retail']."</div></a>";
echo "<div class='clear'></div>";
}

 

Right now it displays:

 

Machine Model 1 Header

Part Brand - Product1 - Price

-------------------------------------------

Machine Model 1 Header

Part Brand - Product2 - Price

-------------------------------------------

Machine Model 2 Header

Part Brand - Product3 - Price

---------------------------------------------

 

and I need it to be:

 

Machine Model 1 Header

Part Brand - Product1 - Price

Part Brand - Product2 - Price

--------------------------------------------

Machine Model 2 Header

Part Brand - Product3 - Price

--------------------------------------------

 

 

Link to comment
Share on other sites

That's what I thought. Try this:

 

<?php
$query = "select machineTyoe from ARL2008 where machineMfctr = '$brand'";

$result = mysql_query($query);
if (!$result) {
    die("Query to show fields from table failed");
}
$row = mysql_fetch_array($result) or die(mysql_error());

$MachineModel = ""
while($row = mysql_fetch_array($result)){
if ($row['MachineModel']!=$MachineModel){
	echo "<h3>".$row['MachineModel']."</h3>";
	$MachineModel = $row['MachineModel'];
}
echo "<div class='threeCol emptyBlox'>".$row['Brand']."</div><a class='blox' style='padding:0px;' href='toner/".$Partno.".htm'>
        <div class='threeCol'>".$row['DESCR1']."</div></a><a class='blox' style='padding:0px;' href='toner/".$Partno.".htm'>
        <div class='threeCol'>$".$row['Retail']."</div></a>";
echo "<div class='clear'></div>";
}
?>

 

Not sure where your "--------" line comes in, but that should give you the idea.

Link to comment
Share on other sites

I can get the first result with this code but it still doubles the header for the first model if there is more than one product for it.

 

<?php 
echo "<h3>".$row['MachineModel']."</h3>";
echo "<a class='blox' style='padding:0px;' href='toner/".$row['Partno'].".htm'>
<div class='partHREF'>
<div class='threeCol emptyBlox'>".$row['Brand']."</div>
<div class='threeCol'>".$row['DESCR1']."</div>
<div class='threeCol'>$".$row['Retail']."</div>
<div class='clear'></div></div></a>";

$MachineModel = "";
while($row = mysql_fetch_array($result)){
if ($row['MachineModel']!=$MachineModel){
	echo "<h3>".$row['MachineModel']."</h3>";
	$MachineModel = $row['MachineModel'];
}
echo "<a class='blox' style='padding:0px;' href='toner/".$row['Partno'].".htm'>
<div class='partHREF'>
<div class='threeCol emptyBlox'>".$row['Brand']."</div>
<div class='threeCol'>".$row['DESCR1']."</div>
<div class='threeCol'>$".$row['Retail']."</div>
<div class='clear'></div></div></a>";

}

?>

 

Attached is a photo of the result:

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

You're echoing the header row at the top of your code, then echoing it again in the loop. Get rid of the first one:

 

<?php 
echo "<a class='blox' style='padding:0px;' href='toner/".$row['Partno'].".htm'>
<div class='partHREF'>
<div class='threeCol emptyBlox'>".$row['Brand']."</div>
<div class='threeCol'>".$row['DESCR1']."</div>
<div class='threeCol'>$".$row['Retail']."</div>
<div class='clear'></div></div></a>";

$MachineModel = "";
while($row = mysql_fetch_array($result)){
if ($row['MachineModel']!=$MachineModel){
	echo "<h3>".$row['MachineModel']."</h3>";
	$MachineModel = $row['MachineModel'];
}
echo "<a class='blox' style='padding:0px;' href='toner/".$row['Partno'].".htm'>
<div class='partHREF'>
<div class='threeCol emptyBlox'>".$row['Brand']."</div>
<div class='threeCol'>".$row['DESCR1']."</div>
<div class='threeCol'>$".$row['Retail']."</div>
<div class='clear'></div></div></a>";

}

?>

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.