|
我只知道SQL所以请把我当成一个完整的菜鸟!
3 {+ X% ` V8 j2 O. H& Q我有一个表,里面有很多行,但有些行可以通过id配对,我想合并这些行的返回数组。
& ?5 P& z% ^; _3 q$ J' _ post_id # meta_name # meta_value # ====================================== # bar # 44 # # foo # on # 2 # bar # # 2 # foo # on # 3 # bar # # 3 # foo # off #想象一下,作为一个按比例缩小的版本,我的目标是返回两个结果! B* q. H0 d% B1 n8 K, `3 X
1 # foo # bar # 44 # on 2 # foo # bar # 1 # on基于ID相关并且c2的值为’on’
, z0 O$ z3 [# E& y' Q9 w我的尝试是从一些阅读中得到以下内容,但没有得到0 `' A. m. k! {. C" ~- _) c
SELECT t1.post_id,t2.post_id,t1.meta_name,t2.meta_name,t1.meta_value,t2.meta_value FROM `ecom_page_meta` t1 JOIN `ecom_page_meta` t2 ON t1.post_id = t2.post_id WHERE t1.meta_name = 'featured-products' AND t1.meta_value = 'on'感谢任何帮助! {! y% M( Q1 d1 e
编辑 + p, x8 k; v! x5 ~" K0 m& x1 M
我认为Id由于以下答案:; [* g/ \; F) t* e5 P2 z% C' [
SELECT L.post_id,L.meta_name,R.meta_name,L.meta_value,R.meta_valueFROM `ecom_page_meta` LINNER JOIN ecom_page_meta R ON L.post_id = R.post_id AND L.meta_name = 'featured-products-order AND R.meta_value = 'on' WHERE R.meta_name = 'featured-products' ORDER BY L.meta_value DESC再次感谢。
, W' v- K, s" @2 y/ E
; M: Y; F9 Z% o& W" b+ |9 E/ r0 k8 a 解决方案: ( u5 S b: w5 Y/ `; ~" A8 c+ A
创建测试数据:0 ?: r; F9 d$ |5 {
SQL> create table ecom_page_meta (post_id number not null 2 ,meta_name varchar2(15) not null 3 ,meta_value varchar2(15) not null);Table created.SQL> insert into ecom_page_meta values (1,'bar,四四row created.SQL> insert into ecom_page_meta values (1,'foo','on');1 row created.SQL> insert into ecom_page_meta values (2,'bar1 row created.SQL> insert into ecom_page_meta values (2,'foo','on');1 row created.SQL> insert into ecom_page_meta values (3,'bar1 row created.SQL> insert into ecom_page_meta values (3,'foo','off');1 row created.SQL> commit;Commit complete.查询给出和OP样品匹配结果:
1 ]5 \, J8 {9 F* n' e' M: a, zSQL> select L.post_id,L.meta_name,R.meta_name,L.meta_value,R.meta_value 2 from ecom_page_meta L 3 inner join ecom_page_meta R 4 on L.post_id = R.post_id 5 and L.meta_value 'on and R.meta_value = 'on' 7 where L.meta_name = 'bar'; POST_ID META_NAME META_NAME META_VALUE META_VALUE---------- --------------- --------------- --------------- --------------- 1 bar foo 4444 44444 444444on bar foo 1 on我还是不知道该怎么办WHERE t1.meta_name = 'featured-products怎么做,因为没有包含字符串的示例数据featured-7 K, Q5 ^! _" e: t5 a; C2 N
products”的t1.meta_name。 |
|