Steini Petur
Steini Petur

Reputation: 129

How to display SQL query results in one line?

I am going out of my mind trying to get this to work and here it is, I have 3 tables. I think it's much better to show you the tables are "tblhosting", "tblhostingconfigoptions" and "tblcustomfieldsvalues" It can be seen here in the SQLFiddle setup: http://sqlfiddle.com/#!9/6c153/1

What I am trying to accomplish is making it in one line, see picture:

Below is my SQL query:

SELECT DISTINCT 
t1.domainstatus,
t1.server,
t1.dedicatedip,
t1.packageid,
t1.regdate,
t1.nextduedate,
t2.value,
t2.fieldid,
t3.configid,
t3.qty
FROM tblhosting t1
INNER JOIN tblcustomfieldsvalues t2 
ON t2.relid = t1.id 
INNER JOIN tblhostingconfigoptions t3 
ON t3.relid = t1.id
WHERE t3.configid IN (126,127,128) AND t2.fieldid IN
(83,84,85,86,87,88,90,91,92,93,208) ORDER by t1.id -- I use to have GROUP by t1.id and get 1 line for the 126 but then 127 128 will not be produced.

I'll isolate the <td> to just the problem, it's all inside this PHP code:

$sql = mysql_query($queryText);

while($row = mysql_fetch_array($sql)) {

  $qty = $row['qty'];
  $configid = $row['configid'];

  echo '<td id="sid">' . $row['value'] . '</td>';
  echo '<td id="smip">' . $dedicatedIP . '</td>';
  echo '<td id="10g">'; if ($configid == 126) { echo '4' } echo'</td>'; // the qty matching 126 should display there
  echo '<td id="40g">'; if ($configid == 127) { echo '0'} echo'</td>'; // the qty matching 127 should display there 
  echo '<td id="100g">'; if ($configid == 128) { echo '0' } echo'</td>'; // the qty matching 128 should display there

.... The idea above is to make the $configid match 126 or 127 or 128, and output into the field the row qty accordingly which is 4 0 0 and not output 3 times the "ded13526" and put 4 then next line 0 then next line 0.

Hope it comes off with enough explanation, I didn't want to post the entire code to confuse, it's obvious the IN statement has 80 to 93 then 208 and this exact entry is only 93..

I just don't know what is wrong..

Upvotes: 7

Views: 4908

Answers (2)

dened
dened

Reputation: 4310

If I understand your problem correctly, here is how it can be solved:

SELECT DISTINCT 
t1.domainstatus,
t1.server,
t1.dedicatedip,
t1.packageid,
t1.regdate,
t1.nextduedate,
t2.value,
t2.fieldid,
(SELECT qty FROM tblhostingconfigoptions WHERE relid = t1.id AND configid = 126) AS qty126,
(SELECT qty FROM tblhostingconfigoptions WHERE relid = t1.id AND configid = 127) AS qty127,
(SELECT qty FROM tblhostingconfigoptions WHERE relid = t1.id AND configid = 128) AS qty128
FROM tblhosting t1
JOIN tblcustomfieldsvalues t2 ON t2.relid = t1.id 
WHERE t2.fieldid IN (83,84,85,86,87,88,90,91,92,93,208)
while($row = mysql_fetch_array($sql)) {
  ...
  echo '<td id="sid">' . $row['value'] . '</td>';
  echo '<td id="smip">' . $row['dedicatedip'] . '</td>';
  echo '<td id="10g">' . $row['qty126'] . '</td>';
  echo '<td id="40g">' . $row['qty127'] . '</td>';
  echo '<td id="100g">' . $row['qty128'] . '</td>';
  ...

Upvotes: 1

Sujata Halwai
Sujata Halwai

Reputation: 107

Try this:

SELECT DISTINCT 
t1.domainstatus,
t1.server,
t1.dedicatedip,
t1.packageid,
t1.regdate,
t1.nextduedate,
t2.value,
t2.fieldid,
t3.configid,
t3.qty
FROM tblhosting t1
INNER JOIN tblcustomfieldsvalues t2 
ON t2.relid = t1.id 
INNER JOIN tblhostingconfigoptions t3 
ON t3.relid = t1.id
WHERE t3.configid IN (126,127,128) AND t2.fieldid IN
(83,84,85,86,87,88,90,91,92,93,208) GROUP BY t2.fieldid

Upvotes: 2

Related Questions