列子:当前用户的排名
SELECT u.amount,u.rowNo,g.nickName,g.avatarUrl FROM ( SELECT amount,uid,(@rowNum :=@rowNum + 1) AS rowNo FROM wxml_wdlmember,(SELECT(@rowNum := 0)) b where wxml_wdlmember.gid=$id ORDER BY wxml_wdlmember.amount DESC ) u , wxml_gameauth g WHERE g.uid=u.uid and u.uid = $uid
--排名及号码
SELECT
uid, tzcnt,amount, (@rowNum :=@rowNum + 1) AS rowNo FROM
wxml_wdlmember,(SELECT(@rowNum := 0)) b WHERE gid=2
ORDER BY
tzcnt DESC
SELECT `name`,`user`,b.userid FROM device_manage a LEFT JOIN device_members b ON b.username = a.`user` WHERE a.`status`=2(联合查询,以 a表为主,表b没有的字段为null)
SELECT `name`,`user`,b.userid FROM device_manage a, device_members b WHERE a.`status`=2 AND b.username = a.`user`(联合查询,a表的字段==表b.字段的数据)
列子:查询归还日期还有一天和已经到期的数据--device.uqee.com
SELECT
a.`name`,
a.`user`,
(
TO_DAYS(
FROM_UNIXTIME(a.returntime)
) - TO_DAYS(NOW())
) AS day1,
DATE_FORMAT(
FROM_UNIXTIME(a.returntime),
'%Y-%m-%d'
) AS date
FROM
device_manage a
WHERE
a.`status` = 2
AND (
TO_DAYS(
FROM_UNIXTIME(a.returntime)
) - TO_DAYS(NOW())
) <= 1
二.时间处理
判断一个时间是否在一个区间内
TO_DAYS函数 返回一个从年份0开始的天数
SELECT TO_DAYS(‘1997-10-07′); //参数datetime 类型
datetime转换为时间戳:unix_timestamp(`lastLoginTime`)
unix_timestamp( time ) between unix_timestamp( 'start ') and unix_timestamp( 'end' )
时间戳转换成datetime:FROM_UNIXTIME(registerTime )
SELECT NOW(),CURDATE(),CURTIME()
NOW() |
CURDATE() |
CURTIME() |
|
|
2016-08-01 16:25:46 |
2016-08-01 |
16:25:46 |
mysql 时间戳格式化
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s') as nowdate,FROM_UNIXTIME('1530675408','%Y-%m-%d %H:%i:%s') as sdate,FROM_UNIXTIME(1530675676,'%Y-%m-%d %H:%i:%s') as sdate1