当前位置:首页>正文

django数据库查询出多行结果 mysql怎么取得查询出了多少条行记录

2023-04-12 02:25:36 互联网 未知

mysql怎么取得查询出了多少条行记录

用count函数就可以查看。
比如表名叫test。
要查询表中一共有多少条记录
select count(*) from test

如果按条件查询的话,就正常使用where条件即可
select count(*) from test where id=1

Django如何多表联合统计查询

如果你觉着使用自带ORM查询费劲的话。直接获取数据库连接,然后执行sql语句。

def my_custom_sql(): from django.db import connection, transaction cursor = connection.cursor() # 数据修改操作——提交要求 cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz]) transaction.commit_unless_managed() # 数据检索操作,不需要提交 cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz]) row = cursor.fetchone() return row

多数据

from django.db import connectionscursor = connections[my_db_alias].cursor()# Your code here...transaction.commit_unless_managed(using=my_db_alias)

通常我们不需要手动调用

transaction.commit_unless_managed(

),我们可以这样做:
@commit_on_successdef my_custom_sql_view(request, value): from django.db import connection, transaction cursor = connection.cursor() # Data modifying operation cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [value]) # Since we modified data, mark the transaction as dirty transaction.set_dirty() # Data retrieval operation. This doesnt dirty the transaction, # so no call to set_dirty() is required. cursor.execute("SELECT foo FROM bar WHERE baz = %s", [value]) row = cursor.fetchone() return render_to_response(template.html, {row: row})

如何在数据库表中查询得到多个结果中的一条记录!

select distinct 生日 from table where 生日=1990
或是
select top 1 (distinct 生日) from table where 生日=1990

随便看看