我希望两个显示第二个表'e_value'的结果,它是两个记录(仅来自一列),作为来自第一个表'e_order_item'的select查询的两列。 我还使用参数'collect_id'显示许多订单商品, 所以我想使用表'e_value'的每两个值显示在选择查询上显示的订单商品ID。
例如,我在表格中有这个
+-------------------------------+
| e_order_item |
+-------------------------------+
| oi_id oi_price oi_collect_id |
| 1 100 2 |
| 2 30 2 |
| 3 55 3 |
| 4 70 4 |
| 5 220 2 |
| 6 300 2 |
+-------------------------------+
+----------------------------+
| e_value |
+----------------------------+
| v_id v_value v_oi_id |
| 1 name1 1 |
| 2 surname1 1 |
| 3 name2 2 |
| 4 surname2 2 |
| 5 name3 5 |
| 6 surname3 5 |
+----------------------------+
我想选择collect_id = 2的order_items,我希望结果像这样
+--------------------------------------------------+
| |
+--------------------------------------------------+
| Result |
| oi_id oi_price oi_collect_id name surname |
| 1 100 2 name1 surname1 |
| 2 30 2 name2 surname2 |
| 5 220 2 name3 surname3 |
| 6 300 2 null null |
| |
+--------------------------------------------------+
答案 0 :(得分:0)
以下是查询:
SELECT
t.oi_id,
t.oi_price,
t.oi_collect_id,
LEFT (
GROUP_CONCAT(t.v_value),
IF (
LOCATE(',',GROUP_CONCAT(t.v_value)) = 0,
LENGTH(GROUP_CONCAT(t.v_value)),
LOCATE(',', GROUP_CONCAT(t.v_value)) - 1
)
) 'Name',
RIGHT (
GROUP_CONCAT(t.v_value),
LENGTH(GROUP_CONCAT(t.v_value)) -
IF (
LOCATE(',',GROUP_CONCAT(t.v_value)) = 0,
LENGTH(GROUP_CONCAT(t.v_value)),
LOCATE(',',GROUP_CONCAT(t.v_value))
)
) Surname
FROM
(
SELECT
*
FROM e_order_item
LEFT JOIN e_value ON e_order_item.oi_id = e_value.v_oi_id
WHERE e_order_item.oi_collect_id = 2
ORDER BY oi_id, v_id
) t
GROUP BY t.oi_id;
注意:
以下示例说明了如何从逗号分隔的字符串中获取第一个字符串和第二个字符串。
SET @str := 'A,BCDEFGHIJKL';
SELECT
LEFT(@str,IF(LOCATE(',',@str) = 0, LENGTH(@str),LOCATE(',',@str)-1)) AS StringBeforeComma,
RIGHT(@str,LENGTH(@str)-IF(LOCATE(',',@str)=0,LENGTH(@str),LOCATE(',',@str))) AS StringAfterComma
结果:
StringBeforeComma StringAfterComma
A BCDEFGHIJKL
答案 1 :(得分:0)
您必须转动以获得所需的结果。
select oi_id, oi_price, oi_collect_id
, max(name) as name
, max(surname) as surname
from (
select
i.oi_id, i.oi_price, i.oi_collect_id
, case when @prevVal <> (@currVal:=v.v_oi_id)
then v.v_value
else null
end as name
, case when @prevVal = @currVal
then v.v_value
else null
end as surname
, @prevVal:=@currVal as temp_currVal
from e_order_item i
left join e_value v on v.v_oi_id = i.oi_id,
(select @prevVal:=-1, @currVal:=-1) as inits
where i.oi_collect_id=2
) as main_data
group by oi_id, oi_price, oi_collect_id
order by 1;
答案 2 :(得分:0)
这已经过测试并成功运行...并根据需要提供输出...
有两个子查询:
1.首先会给出collect_id = 2
...
1.SELECT tab1.oi_id, tab1.oi_price, tab1.oi_collect_id
from(
SELECT oi_id, oi_price, oi_collect_id
from e_order_item
where oi_collect_id = 2
) as tab1;
2.此查询将在不同的列中为您提供姓名,姓名和身份证明。
2.(SELECT e.v_value as name, surname, id
from (
select t1.v_value as surname, t1.v_oi_id as id from e_value as t1
group by t1.v_oi_id
)join e_value as e on id = e.v_oi_id and surname <> e.v_value
) as tab2 on tab1.oi_id = tab2.id;
现在left join
这两个查询得到我们想要的结果:
SELECT tab1.oi_id, tab1.oi_price, tab1.oi_collect_id, name, surname
from(
SELECT oi_id, oi_price, oi_collect_id
from e_order_item
where oi_collect_id = 2
) as tab1 left join
(SELECT e.v_value as name, surname, id
from (
select t1.v_value as surname, t1.v_oi_id as id from e_value as t1
group by t1.v_oi_id
)join e_value as e on id = e.v_oi_id and surname <> e.v_value
) as tab2 on tab1.oi_id = tab2.id
order by tab1.oi_id asc; // to print in ascending order..
为什么我们使用left join
..您可以使用此链接http://www.w3schools.com/sql/sql_join_left.asp来正确理解...
如果这个解决方案有用,请告诉我......