我有2张桌子
的OrderItems
+--------+------------+------------------+---------+
| item_id| item_name | Item_type | trans |
+--------+------------+------------------+---------+
| 1 | Devon | line_item | 0 |
| 2 | 100 | coupon | 0 |
| 3 | Contin | line_item | 1 |
+--------+------------+------------------+---------+
orderitems_data
1+--------+------------+------------------+-----------+
| id | item_id | Item_key | key_val |
+--------+------------+------------------+------------+
| 1 | 1 | Delivery | 2015/04/03 |
| 2 | 1 | attrib | pick_up |
| 3 | 1 | qty | 1 |
| 4 | 2 | discount | 0 |
| 5 | 3 | Delivery | 2015/04/15 |
| 5 | 3 | attrib | doorstep |
+--------+------------+------------------+------------+
我需要从'orderitems'中选择'item_name',其中'Item_type = line_item'和'trans = 0','orderitems_data'中的'key_val','item_key = Delivery',以及'key_val',其中'item_key = attribute'用于相同的'item_name'
然后显示它们。我正在努力查询,我想写
SELECT orderitems.item_name AS item_name WHERE orderitems.Item_type='line_item' AND trans=0, orderitems_data.key_val AS deliverydate WHERE Item_key='Delivery', orderitems_data.key_val AS location WHERE Item_key='attrib' FROM orderitems INNER JOIN order_items_data ON orderitems.item_id = orderitems_data.item_id
但显然它不对。
有人能指出我正确的方向吗?
我希望获得的结果是
Devon pick_up 2015/04/03
答案 0 :(得分:1)
这会对你有帮助 -
SELECT orderitems.item_name AS item_name, orderitems_data.key_val AS deliverydate, orderitems_data.key_val AS location from orderitems left join orderitems_data
ON orderitems.item_id = orderitems_data.item_id
WHERE orderitems.Item_type='line_item' AND
orderitems.trans=0 and
( orderitems_data.Item_key='Delivery' or orderitems_data.Item_key='attrib')
答案 1 :(得分:0)
SELECT oi.item_name, oid.key_val
FROM OrderItem oi, Orderitem_Data oid
WHERE oi.item_id = od.item_id
AND oi.item_type = 'line_item'
AND oid.item_key = 'Delivery'
最终没有得到你的要求
答案 2 :(得分:0)
我想这会起作用
SELECT i.item_name, d.Item_key FROM orderitems i LEFT JOIN orderitems_data d on i.item_id =d.item_id WHERE i.Item_type='line_item' AND i.trans=0 AND d.Item_key in('attrib', 'Delivery');
答案 3 :(得分:0)
这是从EAV数据集构建伪规范化枢轴的股票答案:
SELECT i.*
, MAX(CASE WHEN d.item_key = 'attrib' THEN key_val END) attrib
, MAX(CASE WHEN d.item_key = 'Delivery' THEN key_val END) Delivery
, MAX(CASE WHEN d.item_key = 'qty' THEN key_val END) qty
, MAX(CASE WHEN d.item_key = 'discount' THEN key_val END) discount
FROM orderitems i
LEFT
JOIN orderitems_data d
ON d.item_id = i.item_id
GROUP
BY i.item_id;
+---------+-----------+-----------+-------+----------+------------+------+----------+
| item_id | item_name | Item_type | trans | attrib | Delivery | qty | discount |
+---------+-----------+-----------+-------+----------+------------+------+----------+
| 1 | Devon | line_item | 0 | pick_up | 2015/04/03 | 1 | NULL |
| 2 | 100 | coupon | 0 | NULL | NULL | NULL | 0 |
| 3 | Contin | line_item | 1 | doorstep | 2015/04/15 | NULL | NULL |
+---------+-----------+-----------+-------+----------+------------+------+----------+
请注意我之前关于orderitems_data表中id列冗余的注释以及尽可能使用正确数据类型的重要性。
答案 4 :(得分:0)
如果,我没有错,您希望获得依赖Item_type = 'line_item'
和trans = 0
以及od.Item_key = 'Delivery' OR od.Item_key = 'attrib'
,然后使用以下查询
SELECT DISTINCT
o.*,
od.key_val,
od.Item_key
FROM
orderitems o
LEFT JOIN orderitems_data od
ON o.item_id = od.item_id
WHERE o.Item_type = 'line_item'
AND o.trans = 0
AND (od.Item_key = 'Delivery' OR od.Item_key = 'attrib')