如何在一行中显示SQL查询结果?

时间:2015-05-23 05:04:15

标签: php mysql sql

我想出去试图让它发挥作用,而且在这里,我有3张桌子。我认为向你展示这些表格是更好的选择" tblhosting"," tblhostingconfigoptions"和" tblcustomfieldsvalues"这可以在SQLFiddle设置中看到:http://sqlfiddle.com/#!9/6c153/1

我想要完成的是将它放在一行中,见图:

以下是我的SQL查询:

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.

我会将<td>与问题隔离开来,这一切都在这个PHP代码中:

$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

....上面的想法是使$configid匹配126或127或128,并相应地将行qty输出到字段中,这是4 0 0并且不输出3倍&#34; ded13526&#34;然后放4然后下一行0然后下一行0。

希望它有足够的解释,我不想发布整个代码来混淆,显然IN语句有80到93然后208这个确切的条目是只有93 ..

我只是不知道出了什么问题。

2 个答案:

答案 0 :(得分:2)

试试这个:

linear_search :: (Eq a) => a -> [a] -> Maybe Int
linear_search x xs = case filter ((x ==) . snd) $ zip [0..] xs of
                     []       -> Nothing
                     (i, _):_ -> Just i

答案 1 :(得分:1)

如果我理解你的问题,可以解决这个问题:

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