简体   繁体   English

SQL选择id相同的行

[英]SQL select rows where id is the same

I want to select the following values with the same ID. 我想选择以下具有相同ID的值。 I basically have a submit form which stores its values and I want to display each submit. 我基本上有一个提交表单,用于存储其值,我想显示每个提交。 This is how the Databse Table looks like: 这是数据库表的外观:

| id |         name |        value |
------------------------------------
|   1 |     venue_1 |     New York |
|   1 |      time_1 |        12:00 |
|   1 |     costs_1 |          50€ |
|   2 |     venue_1 |       Berlin |
|   2 |      time_1 |        15:00 |
| ... |         ... |          ... |    

I want to display the values like this: 我想显示这样的值:

|      Venue |      Time |    Cost |
------------------------------------
|   New York |     12:00 |     50€ |
|     Berlin |     15:00 |     ... |
|    A Venue |  The time |    cost |

My SQL queries in PHP look like this (Joomla): 我在PHP中的SQL查询如下所示(Joomla):

$db->setQuery('SELECT value FROM subrecords WHERE name = "venue_1"');
$venue = $db->loadColumn();

$db->setQuery('SELECT value FROM subrecords WHERE name = "time_1"');
$time = $db->loadColumn();

But when I loop through them to display them it doesn't match up: 但是当我遍历它们以显示它们时,它们不匹配:

foreach ($venue as $key => $val) {
    $output .= "<tr>
    <td>".$venue[$key]."</td>
    <td>".$time[$key]."</td>
    </tr>";
}
print($output);

I dont see through the logic here because as of now I just select all the venue_1 and time_1 etc and load the columns so I get every value but they need to be linked to each other so I can display them in the right way. 我在这里看不到逻辑,因为到目前为止,我只选择了所有placement_1和time_1等并加载列,所以我得到了每个值,但它们需要相互链接,以便可以正确的方式显示它们。

Where is my mistake? 我的错误在哪里? How can I link them through the ID maybe? 我如何通过ID链接它们?

If something is unclear tell me please I don't want to make it difficult for you to help me :) 如果不清楚,请告诉我,我不想让您难于帮助我:)

Here you can get all in one Query. 在这里,您可以一次查询所有数据。 When you delete the WHERE line you get the hole table 删除WHERE行时,您会得到孔表

SELECT 
  Coalesce ((CASE WHEN name = "venue_1" THEN value END),NULL) Vebue,
  Coalesce ((CASE WHEN name = "time_1" THEN value END),NULL) Time,
  Coalesce ((CASE WHEN name = "cost_1" THEN value END),0) Coast
  FROM subrecords
  WHERE id = 1
  GROUP BY id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM