MySQL查询数据
目录
MySQL查询数据
在本教程中,您将学习如何使用MySQLSELECT
语句从表或视图查询数据。
MySQL SELECT语句简介
使用SELECT
语句从表或视图获取数据。表由行和列组成,如电子表格。 通常,我们只希望看到子集行,列的子集或两者的组合。SELECT
语句的结果称为结果集,它是行列表,每行由相同数量的列组成。
请参阅示例数据库(yiibaidb
)中的以下employees
表的结构。它有8
列:员工人数,姓氏,名字,分机,电子邮件,办公室代码,报告,职位等。
SELECT
语句控制要查看哪些列和行。例如,如果只对所有员工的名字,姓氏和职位感兴趣,或者您只想查看其职位是销售代表的每位员工的信息,则SELECT
语句可帮助您执行这些操作。
我们来看一下SELECT
语句的语法:
SELECT
column_1, column_2, ...
FROM
table_1
[INNER | LEFT |RIGHT] JOIN table_2 ON conditions
WHERE
conditions
GROUP BY column_1
HAVING group_conditions
ORDER BY column_1
LIMIT offset, length;
SELECT
语句由以下列表中所述的几个子句组成:
SELECT
之后是逗号分隔列或星号(*
)的列表,表示要返回所有列。FROM
指定要查询数据的表或视图。JOIN
根据某些连接条件从其他表中获取数据。WHERE
过滤结果集中的行。GROUP BY
将一组行组合成小分组,并对每个小分组应用聚合函数。HAVING
过滤器基于GROUP BY
子句定义的小分组。ORDER BY
指定用于排序的列的列表。LIMIT
限制返回行的数量。
语句中的SELECT
和FROM
语句是必须的,其他部分是可选的。
在随后的教程中将更详细地了解每个子句。在本教程中,我们将重点介绍
SELECT
语句的简单形式用法。
MySQL SELECT语句示例
SELECT
语句允许通过在SELECT
子句中指定逗号分隔列的列表来查询表的部分数据。 例如,如果要仅查看员工的名字,姓氏和职位,请使用以下查询:
SELECT
lastname, firstname, jobtitle
FROM
employees;
即使员工表中有很多列,SELECT
语句只返回表中所有行的三列数据,如下图所示:
mysql> SELECT lastname, firstname, jobtitle FROM employees;
+-----------+-----------+----------------------+
| lastname | firstname | jobtitle |
+-----------+-----------+----------------------+
| Murphy | Diane | President |
| Patterson | Mary | VP Sales |
| Firrelli | Jeff | VP Marketing |
| Patterson | William | Sales Manager (APAC) |
| Bondur | Gerard | Sale Manager (EMEA) |
| Bow | Anthony | Sales Manager (NA) |
| Jennings | Leslie | Sales Rep |
| Thompson | Leslie | Sales Rep |
| Firrelli | Julie | Sales Rep |
| Patterson | Steve | Sales Rep |
| Tseng | Foon Yue | Sales Rep |
| Vanauf | George | Sales Rep |
| Bondur | Loui | Sales Rep |
| Hernandez | Gerard | Sales Rep |
| Castillo | Pamela | Sales Rep |
| Bott | Larry | Sales Rep |
| Jones | Barry | Sales Rep |
| Fixter | Andy | Sales Rep |
| Marsh | Peter | Sales Rep |
| King | Tom | Sales Rep |
| Nishi | Mami | Sales Rep |
| Kato | Yoshimi | Sales Rep |
| Gerard | Martin | Sales Rep |
+-----------+-----------+----------------------+
23 rows in set
注意比较以下两个语句返回列有什么区别 –
语句-1
SELECT lastname, firstname, jobtitle FROM employees;
语句-2
SELECT * FROM employees;
如果要获取employees
表中所有列的数据,可以列出SELECT
子句中的所有列名,或者只需使用星号(*
)表示您想要从表的所有列获取数据,如下查询:
mysql> SELECT * FROM employees;
+----------------+-----------+-----------+-----------+-----------------------+------------+-----------+----------------------+
| employeeNumber | lastName | firstName | extension | email | officeCode | reportsTo | jobTitle |
+----------------+-----------+-----------+-----------+-----------------------+------------+-----------+----------------------+
| 1002 | Murphy | Diane | x5800 | dmurphy@yiibai.com | 1 | NULL | President |
| 1056 | Patterson | Mary | x4611 | mpatterso@yiibai.com | 1 | 1002 | VP Sales |
| 1076 | Firrelli | Jeff | x9273 | jfirrelli@yiibai.com | 1 | 1002 | VP Marketing |
| 1088 | Patterson | William | x4871 | wpatterson@yiibai.com | 6 | 1056 | Sales Manager (APAC) |
| 1102 | Bondur | Gerard | x5408 | gbondur@gmail.com | 4 | 1056 | Sale Manager (EMEA) |
| 1143 | Bow | Anthony | x5428 | abow@gmail.com | 1 | 1056 | Sales Manager (NA) |
| 1165 | Jennings | Leslie | x3291 | ljennings@yiibai.com | 1 | 1143 | Sales Rep |
| 1166 | Thompson | Leslie | x4065 | lthompson@yiibai.com | 1 | 1143 | Sales Rep |
| 1188 | Firrelli | Julie | x2173 | jfirrelli@yiibai.com | 2 | 1143 | Sales Rep |
| 1216 | Patterson | Steve | x4334 | spatterson@yiibai.com | 2 | 1143 | Sales Rep |
| 1286 | Tseng | Foon Yue | x2248 | ftseng@yiibai.com | 3 | 1143 | Sales Rep |
| 1323 | Vanauf | George | x4102 | gvanauf@yiibai.com | 3 | 1143 | Sales Rep |
| 1337 | Bondur | Loui | x6493 | lbondur@yiibai.com | 4 | 1102 | Sales Rep |
| 1370 | Hernandez | Gerard | x2028 | ghernande@gmail.com | 4 | 1102 | Sales Rep |
| 1401 | Castillo | Pamela | x2759 | pcastillo@gmail.com | 4 | 1102 | Sales Rep |
| 1501 | Bott | Larry | x2311 | lbott@yiibai.com | 7 | 1102 | Sales Rep |
| 1504 | Jones | Barry | x102 | bjones@gmail.com | 7 | 1102 | Sales Rep |
| 1611 | Fixter | Andy | x101 | afixter@yiibai.com | 6 | 1088 | Sales Rep |
| 1612 | Marsh | Peter | x102 | pmarsh@yiibai.com | 6 | 1088 | Sales Rep |
| 1619 | King | Tom | x103 | tking@gmail.com | 6 | 1088 | Sales Rep |
| 1621 | Nishi | Mami | x101 | mnishi@gmail.com | 5 | 1056 | Sales Rep |
| 1625 | Kato | Yoshimi | x102 | ykato@gmail.com | 5 | 1621 | Sales Rep |
| 1702 | Gerard | Martin | x2312 | mgerard@gmail.com | 4 | 1102 | Sales Rep |
+----------------+-----------+-----------+-----------+-----------------------+------------+-----------+----------------------+
23 rows in set
它返回employees
表中的所有列和行。应该使用星号(*
)进行测试。建议显式获取数据的列,原因如下:
- 使用星号(
*
)可能会返回不使用的列的数据。 它在MySQL数据库服务器和应用程序之间产生不必要的I/O磁盘和网络流量。 - 如果明确指定列,则结果集更可预测并且更易于管理。 想象一下,当您使用星号(
*
)并且有人通过添加更多列来更改表格数据时,将会得到一个与预期不同的结果集。 - 使用星号(
*
)可能会将敏感信息暴露给未经授权的用户。
在本教程中,您已经了解并熟悉了MySQL SELECT
语句的用法,并通过SELECT
语句从MySQL表中查询数据。
移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:”教程” 选择相关教程阅读
order generic escitalopram brand escitalopram 10mg buy naltrexone for sale
purchase lasix without prescription order acticlate buy ventolin medication
strattera 25mg cheap order zoloft 50mg for sale brand sertraline 50mg
buy urso 150mg online cheap order generic zyban 150mg zyrtec canada
buy azithromycin 250mg without prescription cheap zithromax 250mg neurontin 100mg oral
medicine for ulcer liquid how to stop tooting fart smells like death
deltasone pill deltasone 10mg cheap order amoxicillin 250mg online
birth control pills online prescription micturition syncope nhs top rated semen enhancers
purchase promethazine pill buy erectile dysfunction drugs stromectol liquid
endoscopy images of gastritis vasodilators adverse effects get uti prescription online
duloxetine 40mg uk glucotrol 5mg drug provigil pill
what is lamisil cream used for most common antifungal drugs cheapest online prescription refill
antiviral medication for hsv 2 ivax salamol cfc free inhaler type 2 diabetes cure breakthrough
medication to quit smoking antidepressant list of medicine for arthritis strongest pain relief medication
medroxyprogesterone 5mg pills cheap biltricide 600 mg purchase microzide online cheap
super strong sleeping pills best diet pills from mexico weight loss drugs prescription online
letrozole 2.5 mg cheap albendazole 400 mg pill buy abilify without a prescription
uroxatral us prescription vs over the counter medicines for heartburn by doctor
minocycline where to buy order minocycline 100mg pills ropinirole over the counter
best cure for teenage acne adult acne medication at ulta buy oxcarbazepine sale
clonidine cost buy clonidine 0.1mg sale tiotropium bromide buy online
buy rocaltrol 0.25 mg for sale oral labetalol 100mg order tricor without prescription
trimox 250mg price order arimidex sale biaxin cheap
best college paper writing service real casino games quick hits free online slots
writing paper online write a literature review brand cefixime 200mg
order aspirin online cheap online casinos paying real money gambling meaning
buy lamisil generic terbinafine canada online card games
how to buy trazodone order suhagra 100mg buy clindamycin for sale
cefuroxime 500mg cost buy ceftin 250mg methocarbamol ca
order tadalafil 10mg for sale purchase tadacip for sale indocin 75mg over the counter
order tamoxifen 20mg for sale order tamoxifen 10mg online cheap rhinocort order
order tretinoin cream generic avanafil 200mg cheap stendra pill
cheap clindamycin buy ed meds sildenafil 100mg cheap
lamotrigine 200mg canada lamotrigine 200mg drug buy nemazole tablets
order flagyl 400mg pills buy keflex 250mg without prescription order cephalexin 125mg sale
buy aurogra online order estradiol 2mg pill estrace 1mg generic
buy generic fluconazole for sale forcan without prescription order cipro 1000mg pills
buy aldactone 25mg pill buy finasteride 1mg proscar online buy
essays online to buy buy my essay buy dissertation online
order domperidone 10mg for sale sumycin 500mg without prescription sumycin 250mg canada
cheap ranitidine 150mg zantac cost buy celebrex tablets
buspar order online buy cheap cordarone buy cordarone
sumatriptan 25mg cheap order levaquin pills order avodart 0.5mg pill
order allopurinol 100mg without prescription order crestor 10mg generic rosuvastatin 20mg us
buy esomeprazole sale nexium 40mg drug buy generic topamax
azelastine 10 ml tablet order acyclovir 400mg online cheap order avalide generic
brand pepcid buy generic pepcid online buy tacrolimus 5mg sale
oral warfarin 5mg buy reglan 10mg without prescription buy maxolon paypal
how to buy xenical order diltiazem sale buy diltiazem 180mg pills
order nortriptyline 25 mg order paracetamol 500 mg online cheap anacin 500 mg cost
purchase glimepiride generic buy arcoxia 60mg online buy arcoxia cheap
order inderal generic buy inderal order clopidogrel 150mg sale
alendronate 70mg canada order macrodantin 100 mg pills buy nitrofurantoin tablets
order ozobax generic amitriptyline 10mg price ketorolac price
loratadine pills buy loratadine medication dapoxetine 90mg brand
baclofen 25mg usa buy amitriptyline 10mg online cheap buy ketorolac generic
phenytoin 100 mg over the counter flexeril online buy generic oxybutynin
buy coversyl generic desloratadine brand purchase fexofenadine
buy levitra 20mg sale cost zanaflex tizanidine ca
methylprednisolone 8mg over counter methylprednisolone 8 mg online buy aristocort without prescription
clomiphene 50mg price imuran over the counter imuran 25mg drug
online casino real money no deposit amoxiclav oral synthroid 150mcg us
order symmetrel for sale symmetrel 100mg uk aczone 100mg cost
real money slots free spins no deposit stromectol 12mg without prescription stromectol coupon
parx casino online play slots ventolin online buy
buy protonix 40mg online cheap order protonix 20mg for sale phenazopyridine pills
play online blackjack real money casino real money buy generic furosemide 40mg
order azipro cost prednisolone 20mg buy neurontin 800mg
oral atorvastatin 10mg order generic albuterol amlodipine 5mg pills
buy isotretinoin 20mg without prescription accutane usa order generic azithromycin 500mg
buy generic cefdinir over the counter glycomet 500mg us prevacid online order
order modafinil 200mg online buy modafinil online cheap buy prednisone pills for sale
order tadalafil 10mg order sildenafil 50mg sildenafil mail order
order micardis 80mg online cheap cost plaquenil molnupiravir canada
buy omeprazole for sale order omeprazole generic buy generic metoprolol
purchase premarin without prescription buy premarin 0.625mg sildenafil buy online
buy generic latanoprost purchase zovirax without prescription buy rivastigmine 6mg generic
buy vasotec 10mg sale bicalutamide where to buy duphalac uk
buy pyridostigmine generic maxalt 5mg price buy maxalt online
ferrous 100 mg without prescription sotalol 40mg brand buy betapace pills
buy etodolac 600 mg without prescription cilostazol 100mg generic cilostazol price
buy prasugrel medication dimenhydrinate over the counter buy tolterodine 1mg online cheap
fludrocortisone 100 mcg for sale imodium over the counter imodium 2 mg for sale
buy duphaston for sale purchase dydrogesterone online cheap order jardiance 10mg online cheap
meloset pill aygestin 5mg brand danocrine generic
dipyridamole us order felodipine generic pravastatin pills
aspirin medication buy zovirax paypal zovirax cheap
precose 50mg usa fulvicin price order fulvicin 250mg without prescription
purchase minoxytop online buy flomax medication the blue pill ed
where to buy ketotifen without a prescription how to buy zaditor tofranil online buy
cheap cialis pill cialis india order viagra pill
tricor pills buy fenofibrate 160mg pills order fenofibrate 200mg generic