---
title: Python MySQL Select From
description: "## Select From a Table – Хүснэгтээс өгөгдлийг сонгох To select from a table in MySQL, use the “SELECT” statement: MySQL-д хүснэгтээс өгөгдлийг сонгохын тулд “SELECT” командыг ашиглана: #### Example – Жишээ Select all records from the “customers” table, and…"
url: "https://apprentice.mn/docs/python-mysql-select-from/"
updated: "2024-08-05"
category: Python
---

# Python MySQL Select From

## Select From a Table – Хүснэгтээс өгөгдлийг сонгох

To select from a table in MySQL, use the “SELECT” statement:

MySQL-д хүснэгтээс өгөгдлийг сонгохын тулд “SELECT” командыг ашиглана:

#### Example – Жишээ

Select all records from the “customers” table, and display the result:

“customers” хүснэгтээс бүх мөрийг сонгож үр дүнг харуулах:

`import mysql.connectormydb = mysql.connector.connect(  host="localhost",  user="yourusername",  password="yourpassword",  database="mydatabase")mycursor = mydb.cursor()mycursor.execute("SELECT * FROM customers")myresult = mycursor.fetchall()for x in myresult:  print(x)`

**Note:** We use the `fetchall()` method, which fetches all rows from the last executed statement.

**Анхаар**: `fetchall()` аргыг ашиглаж байна, энэ нь хамгийн сүүлд гүйцэтгэсэн мэдэгдлээс бүх мөрийг авдаг.

## Selecting Columns – Багана сонгох

To select only some of the columns in a table, use the “SELECT” statement followed by the column name(s):

Хүснэгтэд байгаа зарим баганаас л өгөгдлийг сонгохын тулд “SELECT” командыг баганын нэрийн(нүүдийн) хамт ашиглана:

#### Example – Жишээ

Select only the name and address columns:

Зөвхөн `name` болон `address` баганын өгөгдлийг сонгох:

`import mysql.connectormydb = mysql.connector.connect(  host="localhost",  user="yourusername",  password="yourpassword",  database="mydatabase")mycursor = mydb.cursor()mycursor.execute("SELECT name, address FROM customers")myresult = mycursor.fetchall()for x in myresult:  print(x)`

## Using the fetchone() Method – fetchone() Аргыг ашиглах

If you are only interested in one row, you can use the `fetchone()` method.

Зөвхөн нэг дүгээр мөрийн өгөгдлийг авахыг хүсвэл `fetchone()` аргыг ашиглаж болно.

The `fetchone()` method will return the first row of the result:

`fetchone()` арга нь үр дүнгийн эхний мөрийг буцаана:

#### Example – Жишээ

Fetch only one row:

Зөвхөн нэг дүгээр мөрийн өгөгдлийг авах:

`import mysql.connectormydb = mysql.connector.connect(  host="localhost",  user="yourusername",  password="yourpassword",  database="mydatabase")mycursor = mydb.cursor()mycursor.execute("SELECT * FROM customers")myresult = mycursor.fetchone()print(myresult)`

        [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-select-from%2F&linkname=Python%20MySQL%C2%A0Select%20From](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-select-from%2F&linkname=Python%20MySQL%C2%A0Select%20From)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-select-from%2F&linkname=Python%20MySQL%C2%A0Select%20From](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-select-from%2F&linkname=Python%20MySQL%C2%A0Select%20From)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-select-from%2F&linkname=Python%20MySQL%C2%A0Select%20From](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-select-from%2F&linkname=Python%20MySQL%C2%A0Select%20From)[https://www.addtoany.com/share](https://www.addtoany.com/share)
