简体   繁体   English

如何更改查询以获得所需结果?

[英]How to modiff my query to obtain the desired result?

I have two tables (champ_value and champ_form), i tried two queries but i didn't obtain the result that I want, 我有两个表(champ_value和champ_form),我尝试了两个查询,但没有得到想要的结果,

please can you give me other solution 请给我其他解决方案

Thanks in advance. 提前致谢。

+-------------+-----------+---------------------+---------------+
| 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           |
+------------+-------------+---------------+-------------+

the desired result will be like this : 所需的结果将是这样的:

+----------------+-------+-------------+-------------+----------+
| 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 |
+----------------+-------+-------------+-------------+----------+

I tried this first query : 我尝试了第一个查询:

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

and I obtain this : 我得到这个:

+---------------+-------+-------------+-------------+----------+
| 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 |
+---------------+-------+-------------+-------------+----------+

and the second query : 第二个查询:

   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   |
+----------------+-------+-------------+-------------+---------+

The first query gives the good values but not the correct positions and the second gives the correct positions but not the correct values. 第一个查询给出了正确的值,但是没有正确的位置,第二个查询给出了正确的位置,但是没有正确的值。

Without describing the desired behavior is difficult to understand what you want. 没有描述所需的行为很难理解您想要的。 But probably the problem is that the selected columns are not in the GROUP BY . 但是可能的问题是所选列不在GROUP BY

Try the following: 请尝试以下操作:

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

More info here and here . 更多信息在这里这里

This does display according to position - still not convinced it's safe though! 确实会根据位置显示-尽管仍不确信它是安全的!
/* / *

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 

Actual Result 实际结果

+------+-------+-------------+-------------+----------+
| 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 |
+------+-------+-------------+-------------+----------+

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

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