代码:
$.ajax({
url : url,
success : function(data){
var responseHtml = $.parseHTML(data);
songTitle = $.trim(
$(responseHtml).filter('div.head:first').text());
alert(songTitle);
}
});
我想将结果分成如下:
CREATE TABLE table1 (
day DATE,
name VARCHAR(40)
);
INSERT INTO table1 (day, name)
VALUES
('2018-01-01', 'test1'),
('2018-01-01', 'test2'),
('2018-01-01', 'example'),
('2018-01-01', 'somevalue'),
('2018-01-02', 'test3'),
('2018-01-03', 'test4');
而不是在选择数据时复制日期。
预期结果:
day - name1 - name2 - namex
DATE - value - value - value
答案 0 :(得分:1)
您可以通过动态sql执行此操作,首先查找不同的名称值然后围绕它们构建其余代码,例如
给定的
MariaDB [sandbox]> select * from t;
+------------+-----------+
| day | name |
+------------+-----------+
| 2018-01-01 | test |
| 2018-01-01 | test |
| 2018-01-01 | example |
| 2018-01-01 | somevalue |
| 2018-01-02 | test |
| 2018-01-03 | test |
+------------+-----------+
6 rows in set (0.00 sec)
set @sql = concat('select day, ',
(select group_concat(maxstr)
from
(select concat('max(case when name = ', char(39),name,char(39),' then name else null end) as ', concat('name',@rn:=@rn+1)) maxstr
from
(select distinct name from t) t,(select @rn:=0) rn
) s
)
,
' from t group by day')
;
构建此代码
select day, max(case when name = 'test' then name else null end) as name1,
max(case when name = 'example' then name else null end) as name2,
max(case when name = 'somevalue' then name else null end) as name3
from t group by day;
运行时产生此结果
+------------+-------+---------+-----------+
| day | name1 | name2 | name3 |
+------------+-------+---------+-----------+
| 2018-01-01 | test | example | somevalue |
| 2018-01-02 | test | NULL | NULL |
| 2018-01-03 | test | NULL | NULL |
+------------+-------+---------+-----------+
3 rows in set (0.00 sec)
使用动态sql的优点是它非常火,并且忘记代码会捕获任何新值。但请注意group_concat限制。
像这样执行动态sql -
prepare sqlstmt from @sql;
execute sqlstmt;
deallocate prepare sqlstmt;
答案 1 :(得分:0)
答案 2 :(得分:0)
你可以使用group by和group concat按照下面的方式执行此操作
select t.day,left(t.data,length(t.data)-1)
from
(
SELECT day,replace(group_concat(concat(name,'-')),',','')as data
FROM table1
group by day
)t