MYSQL中查讯咋查第一条记录!

MYSQL中查讯咋查第一条记录!,第1张

查看第一条:

SELECT

group_concat(id ORDER BY `create_time` DESC)

FROM

`user`

GROUP BY

user_code

select top 1 * from book 不对,因为mysql里没有top这种写法,它用limit。

扩展资料:

1、查看最后一条

mysql:

select * from table order by id DESC limit 1

oracle:

select * from emp where id in (select max(id) from emp)

2、查询前n行记录

select * from table1 limit 0,n或select * from table1 limit n

3、查询后n行记录

select * from table1 order by id desc dlimit n;//倒序排序,取前n行,id为自增形式

比方说user表里有三个字段,分别是id、name、age,那么当你查找到某一记录时,可以用下面的方法分别取出这三个字段的值:

$conn=new mysqli("xxxxxx这些参数自己搞定xxxx","xxxx","xxxx","xxxx")

$rs=$conn->query("select * from `user` limit 1")

//方法一:

$data=$rs->fetch_assoc()

$id=$data["id"]

$name=$data["name"]

$age=$data["age"]

//方法二:

$data=$rs->fetch_row()

$id=$data[0]

$name=$data[1]

$age=$data[2]

//方法三:

$data=$rs->fetch_object()

$id=$data->id

$name=$data->name

$age=$data->age

//方法四:

list($id,$name,$age)=$rs->fetch_row()

//还有很多方法就不一一列举了

您好,很高兴为您解答。

方法一:

查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):

select * from table_a where id = (select id from table_a where id < {$id} [and other_conditions] order by id desc limit 1) [and other_conditions]

查询下一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):

select * from table_a where id = (select id from table_a where id > {$id} [and other_conditions] order by id asc limit 1) [and other_conditions]

方法二:

查询上一条记录的SQL语句((如果有其他的查询条件记得加上other_conditions以免出现不必要的错误))

select * from table_a where id = (select max(id) from table_a where id < {$id} [and other_conditions]) [and other_conditions]

查询下一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):

select * from table_a where id = (select min(id) from table_a where id > {$id} [and other_conditions]) [and other_conditions]

如若满意,请点击右侧【采纳答案】,如若还有问题,请点击【追问】

希望我的回答对您有所帮助,望采纳!

~ O(∩_∩)O~


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6111089.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-15
下一篇 2023-03-15

发表评论

登录后才能评论

评论列表(0条)

保存