Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php foreach中的mysql查询_Php_Mysql_Sql_Select_Join - Fatal编程技术网

Php foreach中的mysql查询

Php foreach中的mysql查询,php,mysql,sql,select,join,Php,Mysql,Sql,Select,Join,我有一张桌子: 1。ms_字段 +--+------+ |id|name | +--+------+ |1 |Color | +--+------+ |2 |Gender| +--+------+ +--+--------+------+--------+ |id|fieldsid|value |ordering| +--+--------+------+--------+ |1 |1 |White |0 | +--+--------+------+--------+

我有一张桌子:

1。ms_字段

+--+------+
|id|name  |
+--+------+
|1 |Color |
+--+------+
|2 |Gender|
+--+------+
+--+--------+------+--------+
|id|fieldsid|value |ordering|
+--+--------+------+--------+
|1 |1       |White |0       |
+--+--------+------+--------+
|2 |1       |Black |1       |
+--+--------+------+--------+
|3 |1       |Orange|2       |
+--+--------+------+--------+
|4 |1       |Green |3       |
+--+--------+------+--------+
|5 |1       |Blue  |4       |
+--+--------+------+--------+
|6 |2       |Male  |0       |
+--+--------+------+--------+
|7 |2       |Female|1       |
+--+--------+------+--------+
2。ms_字段_值

+--+------+
|id|name  |
+--+------+
|1 |Color |
+--+------+
|2 |Gender|
+--+------+
+--+--------+------+--------+
|id|fieldsid|value |ordering|
+--+--------+------+--------+
|1 |1       |White |0       |
+--+--------+------+--------+
|2 |1       |Black |1       |
+--+--------+------+--------+
|3 |1       |Orange|2       |
+--+--------+------+--------+
|4 |1       |Green |3       |
+--+--------+------+--------+
|5 |1       |Blue  |4       |
+--+--------+------+--------+
|6 |2       |Male  |0       |
+--+--------+------+--------+
|7 |2       |Female|1       |
+--+--------+------+--------+
我的PHP代码:

function get_fields_and_values() {
   $sql_result = 'SELECT `id`,`name`,`ordering`
   FROM `ms_fields`
   WHERE 
   `id` IN (1,2)
   ORDER BY
   `ordering`
   ASC';

   $rows = $this->db1->query($sql_result)->resultset();

   $fields_list = array();
   if(count($rows)>0) {
      foreach ($rows as $row) {
         $row['values'] = self::load_values($row['id']);
         $fields_list[] = $row;
      } 
   }
}


 function load_values($fid) {
    $sql_result = 'SELECT 
    id, 
    fieldsid,
    value,
    ordering
    FROM 
    `ms_fields_values` 
    WHERE fieldsid = $fid';

    $rows = $this->db1->query($sql_result)->resultset();

    $values_list = array();

    if(count($rows)>0) {
     foreach ($rows as $row) {      
      $values_list[] = $row; 
     } 
    }
    return $values_list;
 }
正如您所看到的,我的load_values函数位于foreach语句中。这很糟糕。 是否可以使用1请求更改第一个sql查询以获取字段和值?
谢谢。

使用简单的
JOIN

SELECT f.id, f.namr, fv.id, fv.value, fv.ordering 
FROM ms_fields f
LEFT JOIN ms_fields_values fv ON f.id = fv.fieldsid 
WHERE f.id IN (1, 2)
ORDER BY f.id, fv.ordering;

由于一对多的关系,
foreach
中的查询可能会返回多行,因此您这样做是正确的。您可以
JOIN
将表连接在一起,但它不会以与现在相同的结构为您提供数据,因此您是否这样做取决于您想要数据的方式。