写了个sql,浪费了。

INSERT INTO pw4_grouplogin( gid, gname, views, users, arts, login_num, tuisong, g_date )
select *from ((
SELECT z.gid,z.gname, z.views, z.users, z.arts, z.x, b.q, z.riqi
FROM (

SELECT a.gid, a.gname, a.views, a.users, a.arts, COUNT( DISTINCT b.uid ) AS x, DATE_FORMAT( NOW( ) , '%y-%m-%d' ) AS riqi
FROM pw4_group a
INNER JOIN pw4_user_group b ON a.gid = b.gid
WHERE FROM_UNIXTIME( b.thisvisit, '%y-%m-%d' ) = DATE_FORMAT( NOW( ) , '%y-%m-%d' )
GROUP BY b.gid
) AS z
LEFT JOIN (

SELECT a.gid, a.views, a.users, a.arts, a.gname, count( b.uid ) AS q, date_format( now( ) , '%y-%m-%d' ) AS riqi
FROM pw4_group a
INNER JOIN pw4_group_art b ON a.gid = b.gid
WHERE from_unixtime( b.subtime, '%y-%m-%d' ) = date_format( now( ) , '%y-%m-%d' )
GROUP BY b.gid
) AS b ON z.gid = b.gid
)
UNION (

SELECT b.gid,b.gname, b.views, b.users, b.arts, z.x, b.q, b.riqi
FROM (

SELECT a.gid, a.gname, a.views, a.users, a.arts, COUNT( DISTINCT b.uid ) AS x, DATE_FORMAT( NOW( ) , '%y-%m-%d' ) AS riqi
FROM pw4_group a
INNER JOIN pw4_user_group b ON a.gid = b.gid
WHERE FROM_UNIXTIME( b.thisvisit, '%y-%m-%d' ) = DATE_FORMAT( NOW( ) , '%y-%m-%d' )
GROUP BY b.gid
) AS z
RIGHT JOIN (

SELECT a.gid, a.views, a.users, a.arts, a.gname, count( b.uid ) AS q, date_format( now( ) , '%y-%m-%d' ) AS riqi
FROM pw4_group a
INNER JOIN pw4_group_art b ON a.gid = b.gid
WHERE from_unixtime( b.subtime, '%y-%m-%d' ) = date_format( now( ) , '%y-%m-%d' )
GROUP BY b.gid
) AS b ON z.gid = b.gid
) order by gid) as p
注:mysql不支持全连接。

你可能感兴趣的:(sql,数据库,博客,原创,cao2xi)