数据库必会项目实例(一)

mysql> desc an1;
+---------+-----------+------+-----+---------+----------------+
| Field   | Type      | Null | Key | Default | Extra          |
+---------+-----------+------+-----+---------+----------------+
| aid     | int(11)   | NO   | PRI | NULL    | auto_increment |
| aname   | char(200) | YES  |     | NULL    |                |
| info    | char(200) | YES  |     | NULL    |                |
| user1id | int(11)   | YES  | MUL | NULL    |                |
+---------+-----------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> desc qn1;
+---------+-----------+------+-----+---------+----------------+
| Field   | Type      | Null | Key | Default | Extra          |
+---------+-----------+------+-----+---------+----------------+
| qid     | int(11)   | NO   | PRI | NULL    | auto_increment |
| qname   | char(200) | YES  |     | NULL    |                |
| info    | char(200) | YES  |     | NULL    |                |
| user1id | int(11)   | YES  | MUL | NULL    |                |
| an1id   | int(11)   | YES  | MUL | NULL    |                |
+---------+-----------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

mysql> desc user1n1;
ERROR 1146 (42S02): Table 'zy3.user1n1' doesn't exist
mysql> desc user1;
+----------+-----------+------+-----+---------+----------------+
| Field    | Type      | Null | Key | Default | Extra          |
+----------+-----------+------+-----+---------+----------------+
| userid   | int(11)   | NO   | PRI | NULL    | auto_increment |
| username | char(200) | YES  |     | NULL    |                |
| info     | char(200) | YES  |     | NULL    |                |
+----------+-----------+------+-----+---------+----------------+





import pymysql


conn = pymysql.connect(host='localhost',user='root',password='1111',database='zy3',charset='utf8')

cursor = conn.cursor()

#gender1 = int(input('shuru'))
#sql="""
#     insert into stu(id,gender)
#     values ('%s','%s')
#"""

#sql ="""
#    update stu set gender=%s where id=%s 
#"""

# %(id,gender)1

#查找
#sql = """
#    SELECT * from qn1 WHERE qid=%s 
#"""
#
#cursor.execute(sql,[qid])
#
#mysqlconn.commit()
def select_one_data():
    qid = int(input('shuru'))
    sql = """
        SELECT * FROM qn1 WHERE qid=%s
    """
    print(11)
    
    res = cursor.execute(sql,[qid])
    # res 返回受影响的行数
    print(res)
    #获取一条数据
    print(cursor.fetchone())
    conn.commit()
    

    cursor.close()
    conn.close()



def main():
    select_one_data()

    

if __name__ == '__main__':
    main()

select * from qn1 inner join user1 on qn1.qid=user1.userid;

mysql> select * from an1 inner join user1 on an1.aid=user1.userid;

select * from user1 inner join an1 on an1.aid=user1.userid;

mysql> select username,group_concat(info) from user1 group by username ;

select username,count() from user1 join qn1 on user1.userid=qn1.qid group by username having count()>3;

mysql> select username,count(),group_concat(qn1.info) from user1 join qn1 on user1.userid=qn1.qid group by username having count()>=1;

你可能感兴趣的:(数据库必会项目实例(一))