Biến SQL thành Python

Biến SQL thành Python

Đôi lúc là một Data Analyst chúng ta cần nhảy qua nhảy lại giữa Python và SQL, mỗi lần như vậy đều phải sử dụng google rất tốn thời gian. Mình xin tổng hợp lại một số câu lệnh SQL và Python tương ứng với nhau để giúp mọi người tham khảo.

Mình sẽ viết dưới dạng câu SQL ở trên và câu Python tương ứng ở dưới các bạn có thể ghé thăm bài viết tự học Python nếu các bạn vẫn chưa bắt đầu với Python.

Học python ở đây nhé

Và các bài viết về tự học SQL ở link này :

https://tuananalytic.com/tag/sql-vi/

Bây giờ vào chủ đề chính nhé:

Hàm Select

select all

-- SQL
select * from table
# python
table

select column

-- SQL
select col1 from table
# python
table.col1

select distinct

-- SQL
select distinct * from table
# python
table1.drop_duplicates()

Hàm Where

Where = x

-- SQL
select * from table1 where col1 = 5
# python
table.loc[table['column_name'] == 5]

Where <> x

-- SQL
select * from table1 where col1 <> 5
# python
table.loc[table['column_name'] != 5]

Where > x

-- SQL
select * from table1 where col1 > 5
# python
table1[col1 > 5]

Where chứa string

-- SQL
select * from table1 where col1 like '%string%'
# python
table[‘col1’].str.contains('string')]

Where bắt đầu bằng string

-- SQL
select * from table1 where col1 like 'string%'
# python
table.loc[.str.startswith('string')]

Where kết thúc bằng string

-- SQL
select * from table1 where col1 like '%string'
# python
table.loc[.str.endsswith('string')]

Where null

-- SQL
select * from table1 where col1 is null
# python
table[‘col1’].isna()

Where not null

-- SQL
select * from table1 where col1 is not null
# python
table[‘col1’].notna()

Hàm Update

Update 1 cột có điều kiện

-- SQL
update table set col2 = 'Y' where col1 > 10
# python
table.loc[table.col1 > 10, ‘col2’] = ‘Y’

Update 1 cột nhiều điều kiện

-- SQL
update table set col3 = 'Y' where col1 > 10 and col2 = 'x'
# python
table.loc[(table.col1 > 10) & (table.col2 == ‘x’), ‘col3’] = ‘Y’

Thay bằng 0 khi null

-- SQL
update table set col1 = 0 where col1 is null
# python
table.loc[table[‘col1’].isnull(), ‘col1’] = 0

Thay giá trị cột này bằng cột khác

-- SQL
update table set col2 = col1*100 where col1 > 10
# python
table.loc[table.col1 > 10, ‘col2’] = table.col*100

Hàm Join

Join left một điều kiện

-- SQL
select * from table1 a left join table2 b 
on a.name = b.name
# python
table = pd.merge(table1, table2, left_on = ['name'], right_on = ['name'], how = 'left')

Join right nhiều điều kiện

-- SQL
select * from table1 a right join table2 b 
on a.name = b.name and a.id = b.id
# python
table = pd.merge(table1, table2, left_on = ['name', 'id' ], right_on = ['name', 'id'], how = 'right')

Join Inner nhiều bảng nhiều điều kịen

select * from table1 a inner join table2 b 
on a.name = b.name
inner join table3 c 
on a.name = c.name
# python
table = pd.merge(table1, table2, left_on = ['name'], right_on = ['name'], how = 'inner')
table = pd.merge(table, table3, left_on = ['name'], right_on = ['name'], how = 'inner')

Sort

Sort bảng theo 1 cột

-- SQL
select * from table sort by id asc
# python
table.sort_values(by=[‘id’], inplace=True, ascending = True)

Sort bảng theo nhiều cột

-- SQL
select * from table sort by id, name
# python
table.sort_values(by=[‘id’, ‘name’], inplace=True, ascending = False)

Kết

Vậy là đã hết các nội dung về biên dịch SQL sang python, nếu có thời gian mình sẽ viết thêm các hàm mới nhé, cảm ơn các bạn đã đón đọc.

Leave a Comment

Scroll to Top