多表查询:
连表查询:表(dep,emp)
select * from dep inner join emp on dep.id =emp.dep_id 连接两个表 where dep.name="技术” 查询
inner join 内连接: 数据有对应关系的留下,没有的不显示
先连接表在查询
left join 左连接 以左边的表为准 左边表里都显示出来,右边表没有对应的用null代替
right join 左连接 以右边的表为准 都显示出来 同上
select * from dep left join emp on dep.id =emp.dep_id
union #全连接
select * from dep right join emp on dep.id =emp.dep_id; 左右连接结合一个都不抛弃
子查询:
select name from emp where dep_id =(select id from where name ="技术”);
以一个表的查询结果为搜索条件
python语言中:
通过pymysql 连接数据库进行查询 ,进行一个简单的登录操作
import pymysql
conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",password="123456",database="db1",charset="utf8")#主机IP (127.0.0.1,默认自己的电脑,port=3306,端口)
#cursor=conn.cursor() # 默认正常输出,看不到列名(表的字段)
n=0
while n<3:
cursor=conn.cursor( pymysql.cursors.DictCursor ) #这种形式以键值对方式显示
username=input("请输入您的用户名:")
password=input("请输入您的密码:")
# sql="select * from book where id ='%s' and name='%s'; "%(username,password) 第一种
# ret=cursor.execute(sql) #影响多少行
sql = "select * from book where id =%s and name=%s; "
ret = cursor.execute(sql,[username,password]) #pymysql 提供的可以识别里面的特殊符号
if ret ==0: # ret 代表查询结构影响多少行 为0 代表数据库中没有该用户的信息
print("账号密码输入错误!")
n=n+1
else: #代表数据库中有该用户的信息
print("欢迎登陆!")
break
# cursor.fetchall()#显示所有
# cursor.fetchone()#显示第一行
# cursor.fetchmany(3)#自己选择输出多少行

被折叠的 条评论
为什么被折叠?



