如何修改我的查询以获得所需的结果?

时间:2016-09-01 12:28:21

标签: mysql join

我有两个表(champ_value和champ_form),我尝试了两个查询,但我没有得到我想要的结果,

请你能给我其他解决方案

提前致谢。

+-------------+-----------+---------------------+---------------+
| champ value |           |                     |               |
|             |           |                     |               |
| v_id        | v_value   |  v_fk_order_item_id | v_fk_champ_id |
| 220         | Bernad    | 20000               | 1             |
| 221         | Lagaf     | 20000               | 2             |
| 500         | Vincent   | 20000               | 1             |
| 501         | Pernault  | 20000               | 2             |
+-------------+-----------+---------------------+---------------+


+------------+-------------+---------------+-------------+
| champ_form |             |               |             |
| cf_id      | cf_position | cf_fk_form_id | cf_champ_id |
| 330        | 10          | 1800          | 1           |
| 331        | 12          | 1800          | 2           |
| 630        | 13          | 1800          | 1           |
| 631        | 14          | 1800          | 2           |
+------------+-------------+---------------+-------------+

期望的结果将是这样的:

+----------------+-------+-------------+-------------+----------+
| desired result |       |             |             |          |
|                |       |             |             |          |
| v_id           | cf_id | cf_position | cf_champ_id | v_value  |
| 220            | 330   | 10          | 1           | Bernard  |
| 221            | 331   | 12          | 2           | Lagaf    |
| 500            | 630   | 13          | 1           | Vincent  |
| 501            | 631   | 14          | 2           | Pernault |
+----------------+-------+-------------+-------------+----------+

我尝试了第一个查询:

SELECT v.v_id, cf.cf_id, cf.cf_position, cf.cf_champ_id, v.v_value
FROM champ_form cf
JOIN champ_value v ON v.v_fk_champ_id = cf.cf_champ_id
WHERE cf.cf_fk_form_id =1800
AND v.v_fk_order_item_id =20000
GROUP BY v_id
ORDER BY cf.cf_position

我得到了这个:

+---------------+-------+-------------+-------------+----------+
| group by v_id |       |             |             |          |
|               |       |             |             |          |
| v_id          | cf_id | cf_position | cf_champ_id | v_value  |
| 220           | 330   | 10          | 1           | Bernard  |
| 221           | 330   | 10          | 1           | Vincent  |
| 500           | 331   | 12          | 2           | Lagaf    |
| 501           | 331   | 12          | 2           | Pernault |
+---------------+-------+-------------+-------------+----------+

和第二个查询:

   SELECT v.v_id, cf.cf_id, cf.cf_position, cf.cf_champ_id, v.v_value
    FROM champ_form cf
    JOIN champ_value v ON v.v_fk_champ_id = cf.cf_champ_id
    WHERE cf.cf_fk_form_id =1800
    AND v.v_fk_order_item_id =20000
    GROUP BY cf.cf_id
    ORDER BY cf.cf_position

+----------------+-------+-------------+-------------+---------+
| group by cf_id |       |             |             |         |
|                |       |             |             |         |
| v_id           | cf_id | cf_position | cf_champ_id | v_value |
| 220            | 330   | 10          | 1           | Bernard |
| 221            | 331   | 12          | 2           | Lagaf   |
| 220            | 630   | 13          | 1           | Bernard |
| 221            | 631   | 14          | 2           | Lagaf   |
+----------------+-------+-------------+-------------+---------+

第一个查询给出了良好的值,但没有给出正确的位置 第二个给出正确的位置,但不是正确的值。

2 个答案:

答案 0 :(得分:0)

如果没有描述所需的行为,很难理解你想要什么。但问题可能是所选列不在GROUP BY

尝试以下方法:

SELECT aa.v_id, cc.cf_id, cc.cf_position, cc.cf_champ_id, aa.v_value
FROM champ_value AS aa
INNER JOIN (
    SELECT _aa.v_id
    FROM champ_value AS _aa
    INNER JOIN champ_form AS _bb
    ON _aa.v_fk_champ_id = _bb.cf_champ_id
    WHERE _aa.cf_fk_form_id = 1800 AND _bb.v_fk_order_item_id = 20000
    GROUP BY _aa.v_id
) AS bb
ON aa.v_id = bb.v_id
INNER JOIN champ_form AS cc
ON aa.v_fk_champ_id = cc.cf_champ_id
ORDER BY aa.cf_position

更多信息herehere

答案 1 :(得分:0)

这根据位置显示 - 但仍然不相信它是安全的!
  / *

create table champ_value ( v_id int,        v_value   varchar(10),  v_fk_order_item_id int, v_fk_champ_id int);
truncate table champ_value;
insert into champ_value values
(220         , 'Bernad'    , 20000               , 1             ),
(221         , 'Lagaf'     , 20000               , 2             ),
(500         , 'Vincent'   , 20000               , 1             ),
(501         , 'Pernault'  , 20000               , 2             );
create table  champ_form(cf_id int,cf_position int, cf_fk_form_id int ,cf_champ_id int);
insert into champ_form values
(330        , 10          , 1800          , 1           ),
(331        , 12          , 1800          , 2           ),
(630        , 13          , 1800          , 1           ),
(631        , 14          , 1800          , 2           );

+----------------+-------+-------------+-------------+----------+
| desired result |       |             |             |          |
|                |       |             |             |          |
| v_id           | cf_id | cf_position | cf_champ_id | v_value  |
| 220            | 330   | 10          | 1           | Bernard  |
| 221            | 331   | 12          | 2           | Lagaf    |
| 500            | 630   | 13          | 1           | Vincent  |
| 501            | 631   | 14          | 2           | Pernault |
+----------------+-------+-------------+-------------+----------+

*/

SELECT s.v_id,t.cf_id, t.cf_position ,t.cf_champ_id ,s.v_value
FROM
(
SELECT I.v_id,v_value,v_fk_order_item_id,v_fk_champ_id,
            @RN:=@RN + 1 RN
FROM     (SELECT @RN:=0) RN,champ_value I
order       by i.v_id asc
) S 
LEFT OUTER JOIN
(SELECT    cf_id,cf_position, cf_fk_form_id,cf_champ_id,
            @RN1:=@RN1 + 1 RN1
FROM     (SELECT @RN1:=0) RN1, champ_form E
order       by e.cf_id asc
) T ON T.RN1 = S.RN 

实际结果

+------+-------+-------------+-------------+----------+
| v_id | cf_id | cf_position | cf_champ_id | v_value  |
+------+-------+-------------+-------------+----------+
|  220 |   330 |          10 |           1 | Bernad   |
|  221 |   331 |          12 |           2 | Lagaf    |
|  500 |   630 |          13 |           1 | Vincent  |
|  501 |   631 |          14 |           2 | Pernault |
+------+-------+-------------+-------------+----------+