join语句横向连接表格,这次讲使用
union语句竖向连接表格。竖向连接表格就是把有相同列的数据竖向连接在一起,在sqlite中有两种竖向连接表格的语句,第一种是直接使用
union语句连接两个查询语句,第二种是使用
union all语句连接两个查询语句,这两个语句的区别是第一种不会连接重复的行,第二种会连接重复的行。
union语句连接表格的语句形式如下:
select ... from table_name where condition
union [all]
select ... from table_name where condition
""" union竖向连接表格
union 连接不重复的行
union all 连接重复的行
the statistics of this file:
lines(count) understand_level(h/m/l) classes(count) functions(count) fields(count)
000000000051 ----------------------l 00000000000000 0000000000000001 ~~~~~~~~~~~~~
"""
import time
import sqlite3
__author__ = '与C同行'
def print_pretty_outcome(cursor):
print('-' * 80)
all_outcome = cursor.fetchall()
for column_desc in cursor.description:
print(f'{column_desc[0]:<20}', end='')
print()
for item in all_outcome:
for cell in item:
try:
print(f'{cell:<20}', end='')
except TypeError:
cell = 'None'
print(f'{cell:<20}', end='')
print()
print('-' * 80)
print()
if __name__ == '__main__':
print(f'当前时间:{time.ctime()}')
conn = sqlite3.connect('learn.db')
c = conn.cursor()
print()
print('union连接多个表格,不重复相同行')
c.execute('select name from report_tb union select name from salary_tb')
print_pretty_outcome(c)
print('union all连接多个表格,重复相同行')
c.execute('select name from report_tb'
' union all select name from salary_tb')
print_pretty_outcome(c)
c.close()
conn.close()

从上面的结果中可以清晰地看到使用uinon
和union all
的区别。

文章转载自与C同行,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




