Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display SQL query results in one line?

Tags:

sql

php

mysql

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..

like image 911
Steini Petur Avatar asked May 23 '15 05:05

Steini Petur


People also ask

How do I display query results in SQL?

You have the option of displaying your query results on the Run SQL window, as opposed to Data Display windows. To do this, go to View > Data Grid (Ctrl+G). Once you have selected this option, a panel will appear at the bottom of the window - your query results will be displayed there.

How do I display two rows in one row in SQL?

You can concatenate rows into single string using COALESCE method. This COALESCE method can be used in SQL Server version 2008 and higher. All you have to do is, declare a varchar variable and inside the coalesce, concat the variable with comma and the column, then assign the COALESCE to the variable.

How do I show SQL results vertically?

You can output query results vertically instead of a table format. Just type \G instead of a ; when you end you queries in mysql prompt.


2 Answers

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
like image 151
Sujata Halwai Avatar answered Oct 06 '22 05:10

Sujata Halwai


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>';
  ...
like image 35
dened Avatar answered Oct 06 '22 05:10

dened