简体   繁体   English

MySQL在单个表中获取多行

[英]MySQL get multiple rows in a single table

Here is the stucture of the DB table that was handed to me. 这是交给我的DB表的结构。 No Id, just name and value. 没有ID,只有名称和值。

|option_name  | option_value|
 | Item1      | Value |
 | Item2      | Value |
 | Item3      | Value |
 | 2ndItem1   | Value |
 | 2ndItem2   | Value |
 | 2ndItem3   | Value |
 | ThirdItem1 | Value |
 | ThirdItem1 | Value |
 | ThirdItem1 | Value |

The statement I would like to have would put the data to something like one of these: 我想要的语句会将数据放入其中之一:

| item1.value | 2ndItem1.value | ThirdItem1.value|
| item2.value, 2ndItem2, ThirdItem2|

There is only a handful of names that I need to match together (about 7-8) so it's not a big deal to match them together by hand if that's what it takes 我只需要匹配几个名称(大约7-8个),所以如果需要,将它们手工匹配就没什么大不了的

Not sure if a JOIN is the right tool for the job, or a loop or.. well I'll let you give me the advice. 不知道JOIN是适合该工作的工具,还是循环或..好吧,我会让您给我建议。

I am going to be using this in a Drupal Migration, which is why I'd like to create this as a MYSQL query. 我将在Drupal迁移中使用它,这就是为什么我想将其创建为MYSQL查询的原因。

In terms of the SQL side of things it sounds like you should be able to do something like: 就SQL方面而言,听起来您应该能够执行以下操作:

SELECT `option_value` FROM table_name WHERE `option_name` in ('item1', 'item2', 'item3', 'item4');

In PHP it should work out to basically be: 在PHP中,它应该基本上是:

<?php
$items = array('item1', 'item2', 'item3', 'item4');
$itemsStr = implode(',', $users); // returns 'item1','item2','item3','item4'
$sql = "SELECT `option_value` FROM table_name where `option_name` in ({$itemsStr})";

This is very rough but hopefully will get you going in the right direction. 这很粗糙,但希望能使您朝正确的方向前进。 I'm sure my use of backticks is in the wrong spots 我确定我使用反引号的位置不对

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

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