0

我有这 3 个 sql 问题:

$producent = mysql_fetch_array(mysql_query("
            SELECT FieldValue 
            FROM `xy8vx_rsform_submission_values`
            WHERE SubmissionId = '$id' AND FieldName = 'Producent'
            "));

$model = mysql_fetch_array(mysql_query("
            SELECT FieldValue 
            FROM `xy8vx_rsform_submission_values`
            WHERE SubmissionId = '$id' AND FieldName = 'Model'
            "));

$nr_ser= mysql_fetch_array(mysql_query("
            SELECT FieldValue 
            FROM `xy8vx_rsform_submission_values`
            WHERE SubmissionId = '$id' AND FieldName = 'Nr seryjny'
            "));

是否可以通过一个 sql 问题获得这 3 个值?

4

5 回答 5

4

是的,使用 IN 子句。

 SELECT FieldName , FieldValue 
 FROM `xy8vx_rsform_submission_values`
 WHERE SubmissionId = '$id' AND FieldName IN ('Producent', 'Model', 'Nr seryjny')

当然,您需要使用循环来获取结果。

// $sql is the above sql
$res = mysql_query($sql);
$result = array();
while ($row = mysql_fetch_assoc($res)) {
  $result[$row['FieldName']] = $row['FieldValue'];
}
print_r($result);
于 2012-10-07T12:33:26.913 回答
1
$id_1=x;
$id_2=y;
$id_3=z;

$name_1="NAME X";
$name_2="NAME Y";
$name_3="NAME Z";


SELECT FieldValue 
FROM `xy8vx_rsform_submission_values`
WHERE ((SubmissionId = $id_1 AND FieldName = $name_1) or (SubmissionId = $id_2 AND FieldName = $name_2) or (SubmissionId = $id_3 AND FieldName = $name_3));

现在,您必须将结果提取到 3 行数组中。

于 2012-10-07T12:37:40.910 回答
1

学习和使用SQL IN 运算符

于 2012-10-07T12:41:08.050 回答
1

要获取 3 行中的 3 个值:

SELECT FieldValue 
FROM `xy8vx_rsform_submission_values`
WHERE SubmissionId = '$id' AND 
FieldName in ('Producent', 'Model', 'Nr seryjny')

或者,获取 3 列中的 3 个值:

SELECT
    (SELECT FieldValue 
    FROM `xy8vx_rsform_submission_values`
    WHERE SubmissionId = '$id' AND FieldName = 'Producent') as Producent,

    (SELECT FieldValue 
    FROM `xy8vx_rsform_submission_values`
    WHERE SubmissionId = '$id' AND FieldName = 'Model') as Model,

    (SELECT FieldValue 
    FROM `xy8vx_rsform_submission_values`
    WHERE SubmissionId = '$id' AND FieldName = 'Nr seryjny') as NrSeryjny
于 2012-10-07T12:33:42.270 回答
0

单个查询将产生这 3 个查询的结果

$producent = mysql_fetch_array(mysql_query("
        SELECT FieldValue 
        FROM `xy8vx_rsform_submission_values`
        WHERE SubmissionId = '$id' AND (FieldName = 'Producent' OR FieldName = 'Model' OR FieldName = 'Nr seryjny')
        "));
于 2012-10-07T12:34:40.900 回答