1. 基本概念 (クエリ文)
① 基本文
1. "select * from table name; ”, — テーブル内のすべてのデータをクエリできます;
2. “テーブル名からフィールド名を選択します;”, — テーブル内の指定されたフィールドのデータをクエリできます;
3. “個別のフィールドを選択しますname from table name;” ;”, - テーブル内のデータに対して重複排除クエリを実行できます。
4. "select field name from table name where querycondition;", - テーブル内の指定されたフィールドのデータを条件に従ってクエリできます;
②条件クエリ
1) 比較演算子: >、=、
ユーザーの情報をクエリする18 歳以上
select * from students where age>18; select id, name,gender from students where age>18;18 歳未満の情報を照会
select * from students where age<18;18 歳であるすべての生徒の名前を照会
select * from students where age=18;
2) 論理演算子: and、or、not
–18 ~ 28 歳の学生情報
select * from students where age>18_and age<28:–18 歳以上の女性
select * from students where age>18 and gender="女"; select * from students where age>18 and gender=2;–18 歳以上、または身長が 180 を超えていることが確認されています (両端を含む)
select * from students where age>18 or height>=180;18 歳以上の女性の範囲外の情報
select * from students where not (age>18 and gender=2);年齢は異なります18 以下で、女性です
select * from students where (not age<=18) and gender=2;
3) ファジー クエリ: like、rlike
% 1 つ以上を置き換えます
_ 1 を置き換えます
開始名が「小さい」の名前をクエリしますselect name from students where name="小"; select name from students where name like"小%";名前に「小」が含まれるすべての名前をクエリします
select name from students whece name like "%小%";2 文字の名前をクエリします
select name from students where name like "__";3 文字の名前をクエリ
select name from students where name like "__";少なくとも 2 文字の名前をクエリselect name from
students where name like "__%";#rlike RegularZhou で始まる名前をクエリ#
select name from students where name rlike "^周.*";
Zhou で始まり Lun で終わるクエリ名select name from students where name rlike "^周.*伦$";#4) 範囲クエリ: in、not in、 between…and、not between…and
#年齢が 18 歳から 34 歳までのユーザーをクエリします。名前
select name, age from students where age=18 or age=34; select name,age from students where age in (18,34);非連続範囲内ではありません
年齢が 18 歳から 34 歳の間ではありませんselect name,age from students where age not in (18,34);… と…連続範囲内にあることを示します年齢が 18 ~ 34 歳の人々に関する情報をクエリします
select name,age from students where age between 18 and 34;… と…select * from students where age not between 18 and 34;
の間ではありませんNull 判定
判定が null高さ null に関する情報のクエリ
select *from students where height is null/NULL/Null;判定が null でない is not null
select * from students where height is not null;並べ替え: order_by
–18 ~ 34 歳の男性をクエリし、年齢順に最年少から最年長の順に並べ替えます。Sortselect * from students where (age between 18 and 34) and gender=1;
select * from students where (age between 18 and 34) and gender=1 order by age;
select * from students where (age between 18 and 34) and gender=1 order by age asc;
18 ~ 34 歳の女性をクエリします。歳、身長の高い方から低い方へ並べ替えます
select * from students where (age between18 and 34) and gender=2 order by height desc;
複数のフィールドで並べ替えます18 歳から 34 歳までの女性をクエリします女性の場合、身長は高い方から低い方へ並べ替えられます。
select * from students where (age between 18 and 34) and gender=2 order by height desc,age asc;18 歳から 34 歳までの女性をクエリすると、身長は最も高いものから最も低いものへと並べ替えられます。同じ状況の場合、身長は並べ替えられます。年齢が同じ場合は、小さい順に年齢順に並べ替えます。年齢が同じ場合は、大きい順から小さい順に ID で並べ替えます。
select * from students where (age between 18 and 34) and gender=2 order by height desc,age asc, id desc;年齢順に小さい順、身長が高い順に並べ替えます
select * from students order by age asc,height desc;Group: group_by, group_concat(): クエリの内容、having
where: データテーブル全体の情報の判断です; having: それはグループ化後の判断 データを判断する –group by
性別ごとにグループ化し、すべての性別をクエリするselect gender from students group by gender;–各性別の人数を計算する
select gender, count(*) from students group by gender;
はどこにあるのかフロントでグループ化
–男性の数を計算select count(*) from students where gender='男';–group_concat(…)
同性の名前をクエリselect gender,group_concat(name) from students group by gender;
having:haveはグループの後にありますby平均年齢が 30 歳以上の性別とその名前をクエリしますselect gender ,avg(age) from students group by gender having avg(age) > 30;
各性別に 2 人以上いる情報をクエリしますselect gender,count(*) from students group by gender having count(*) > 2;– クエリ各性別グループの平均年齢
select gender,avg(age) from students group by gender;ページング: 制限limit start,count (start:表示从哪─个开始;count:表示数量) 即limit(第N页-1)*每个的个数,每页的个数; limit在使用的时候,要放在最后面.
クエリされるデータの数を制限するselect *from students where gender=1 limit 2;
Query最初の 5 データ
select* from students limit 0,5;
ID6-10 の書籍シーケンスをクエリ (両端を含む)-合計数--countselect * from students limit 5,5;ページごとに 2 つ表示、最初のページselect * from students limit 0,2;ページごとに 2 つ表示、 2ページ目select * from students limit 2,2;各ページに2つ表示、3ページ目select * from students limit 4,2;各ページに2つ表示、4ページ目select * from students limit 6,2;各ページに2つ表示、6ページ目を表示ページ情報。小さいものから大きいものまで年齢順に並べ替えられていますselect * from students order by age asc limit 10,2;– 並べ替えると、最初のページが表示されますselect * from students where gender=2 order by height des limit 0,2;5) 集計関数: count()、max() 、min()、sum()、avg()、round()
集計関数
-男性の数と女性の数をクエリします。
select count(*) from students where gender=1; select count(*) as 男性人数 from students where gender=1; select count(*) as 女性人数 from students where gender=2;
-最大値-最小値– max --min
最高齢年齢のクエリselect max (age) from students;–女性の最高身長のクエリ
select max (height) from students where gender=2;
-Sum
–sum-すべての人の年齢の合計を計算しますselect sum ( age) from students;–average– avg
–平均年齢を計算しますselect avg(age) from students;–平均年齢を計算するselect sum ( age) / count(* ) from students;–四舍五入round ( 123.23 ,_1)保留1位小数
–计算所有人的平均年龄,保留2位小数select round (sum(age)/count(*),2) from students; select round ( sum(age)/count(*),3) from students ;–计算男性的平均身高保留2位小数
select round(avg (height),2) from students where gender=1; select name,round(avg(height),2) from students where gender=1;
6)连接查询 :inner join, left join, right join
inner join
select … from 表 A inner join表B;select * from students inner join classes;查询有能够对应班级的学生以及班级信息
select * from students inner join classes on students.cls_id=classes.id;按照要求显示姓名、班级
select students.*, classes.name from students inner join classes on students.cls_id=classes.id; select students.name,classes.name from students inner join classes on students.cls_id=classes.id;给数据表起名字
select s.name,c.name from students as s inner join classes as c on s.cls_id=c.id;查询有能够对应班级的学生以及班级信息,显示学生的所有信息,只显示班级名称
select s.*,c.name from students as s inner join classes as c on s.cls_id=c.id;在以上的查询中,将班级姓名显示在第1列
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id;查询有能够对应班级的学生以及班级信息,按照班级进行排序
select c.xxx s.xxx from student as s inner join clssses as c on … order by …;select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;当时同一个班级的时候,按照学生的id进行从小到大排序
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id;left join
查询每位学生对应的班级信息select * from students as s left join classes as c on s.cls_id=c.id;查询没有对应班级信息的学生
– select … from xxx as s left join xxx as c on… where …
– select … from xxx as s left join xxx as c on… . … having …select * from students as s left join classes as c on s.cls_id=c.id having c.id is null; select * from students as s left join classes as c on s.cls_id=c.id where c.id is null;
left join是按照左边的表为基准和右边的表进行查询,查到就显示,查不到就显示为null
补充
查询所有字段:select * from 表名;
查询指定字段:select 列1,列2,... from 表名;
使用 as 给字段起别名: select 字段 as 名字.... from 表名;
查询某个表的某个字段:select 表名.字段 .... from表名;
可以通过 as 给表起别名: select 别名.字段 .... from 表名 as 别名;
消除重复行: distinct 字段
注意:WHERE子句中是不能用聚集函数作为条件表达式的!
二、总结
1、普通查询
(1)命令:select * from ;
(2)命令:select from ;
2、去重查询(distinct)
命令:select distinct from
3、排序查询(order by)
升序:asc
降序:desc
降序排列命令:select from order by desc
不加desc一般默认为升序排列
4、分组查询(group by)
命令:select , Sum(score) from group by
假设现在又有一个学生成绩表(result)。要求查询一个学生的总成绩。我们根据学号将他们分为了不同的组。
命令:
select id, Sum(score) from result group by id;
现在有两个表学生表(stu)和成绩表(result)。
5.等值查询
当连接运算符为“=”时,为等值连接查询。
现在要查询年龄小于20岁学生的不及格成绩。
select stu.id,score from stu,result where stu.id = result.id and age < 20 and score < 60;
等值查询效率太低
6.外连接查询
①语法
select f1,f2,f3,.... from table1 left/right outer join table2 on 条件;
②左外连接查询,例如
select a.id,score from (select id,age from stu where age < 20) a (过滤左表信息) left join (select id, score from result where score < 60) b (过滤右表信息) on a.id = b.id;
左外连接就是左表过滤的结果必须全部存在。右表中如果没有与左表过滤出来的数据匹配的记录,那么就会出现NULL值
③右外连接查询,例如
select a.id,score from (select id,age from stu where age < 20) a (过滤左表信息) right join (select id, score from result where score < 60) b (过滤右表信息) on a.id = b.id;
右外连接就是左表过滤的结果必须全部存在
7.内连接查询
①语法
select f1,f2,f3,.... from table1 inter join table2 on 条件;
②例如
select a.id,score from (select id,age from stu where age < 20) a (过滤左表信息) inner join (select id, score from result where score < 60) b (过滤右表信息) on a.id = b.id;
8.合并查询
在图书表(t_book)和图书类别表(t_bookType)中
①.union
使用union关键字是,数据库系统会将所有的查询结果合并到一起,然后去掉相同的记录;
select id from t_book union select id from t_bookType;
②.union all
使用union all,不会去除掉重复的记录;
select id from t_book union all select id from t_bookType;
以上がMySQL にはどのようなデータ クエリ ステートメントがありますか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

MySQLとSQLiteの主な違いは、設計コンセプトと使用法のシナリオです。1。MySQLは、大規模なアプリケーションとエンタープライズレベルのソリューションに適しており、高性能と高い並行性をサポートしています。 2。SQLiteは、モバイルアプリケーションとデスクトップソフトウェアに適しており、軽量で埋め込みやすいです。

MySQLのインデックスは、データの取得をスピードアップするために使用されるデータベーステーブル内の1つ以上の列の順序付けられた構造です。 1)インデックスは、スキャンされたデータの量を減らすことにより、クエリ速度を改善します。 2)B-Tree Indexは、バランスの取れたツリー構造を使用します。これは、範囲クエリとソートに適しています。 3)CreateIndexステートメントを使用して、createIndexidx_customer_idonorders(customer_id)などのインデックスを作成します。 4)Composite Indexesは、createIndexIDX_CUSTOMER_ORDERONORDERS(Customer_Id、Order_date)などのマルチコラムクエリを最適化できます。 5)説明を使用してクエリ計画を分析し、回避します

MySQLでトランザクションを使用すると、データの一貫性が保証されます。 1)StartTransactionを介してトランザクションを開始し、SQL操作を実行して、コミットまたはロールバックで送信します。 2)SavePointを使用してSave Pointを設定して、部分的なロールバックを許可します。 3)パフォーマンスの最適化の提案には、トランザクション時間の短縮、大規模なクエリの回避、分離レベルの使用が合理的に含まれます。

MySQLの代わりにPostgreSQLが選択されるシナリオには、1)複雑なクエリと高度なSQL関数、2)厳格なデータの整合性と酸コンプライアンス、3)高度な空間関数が必要、4)大規模なデータセットを処理するときに高いパフォーマンスが必要です。 PostgreSQLは、これらの側面でうまく機能し、複雑なデータ処理と高いデータの整合性を必要とするプロジェクトに適しています。

MySQLデータベースのセキュリティは、以下の測定を通じて達成できます。1。ユーザー許可管理:CreateUSERおよびGrantコマンドを通じてアクセス権を厳密に制御します。 2。暗号化された送信:SSL/TLSを構成して、データ送信セキュリティを確保します。 3.データベースのバックアップとリカバリ:MySQLDUMPまたはMySQLPumpを使用して、定期的にデータをバックアップします。 4.高度なセキュリティポリシー:ファイアウォールを使用してアクセスを制限し、監査ロギング操作を有効にします。 5。パフォーマンスの最適化とベストプラクティス:インデックス作成とクエリの最適化と定期的なメンテナンスを通じて、安全性とパフォーマンスの両方を考慮に入れます。

MySQLのパフォーマンスを効果的に監視する方法は? MySqladmin、ShowGlobalStatus、PerconAmonitoring and Management(PMM)、MySQL EnterpriseMonitorなどのツールを使用します。 1. mysqladminを使用して、接続の数を表示します。 2。showglobalstatusを使用して、クエリ番号を表示します。 3.PMMは、詳細なパフォーマンスデータとグラフィカルインターフェイスを提供します。 4.mysqlenterprisemonitorは、豊富な監視機能とアラームメカニズムを提供します。

MySQLとSQLServerの違いは次のとおりです。1)MySQLはオープンソースであり、Webおよび埋め込みシステムに適しています。2)SQLServerはMicrosoftの商用製品であり、エンタープライズレベルのアプリケーションに適しています。ストレージエンジン、パフォーマンスの最適化、アプリケーションシナリオの2つには大きな違いがあります。選択するときは、プロジェクトのサイズと将来のスケーラビリティを考慮する必要があります。

高可用性、高度なセキュリティ、優れた統合を必要とするエンタープライズレベルのアプリケーションシナリオでは、MySQLの代わりにSQLServerを選択する必要があります。 1)SQLServerは、高可用性や高度なセキュリティなどのエンタープライズレベルの機能を提供します。 2)VisualStudioやPowerbiなどのMicrosoftエコシステムと密接に統合されています。 3)SQLSERVERは、パフォーマンスの最適化に優れた機能を果たし、メモリが最適化されたテーブルと列ストレージインデックスをサポートします。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

Dreamweaver Mac版
ビジュアル Web 開発ツール

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

ホットトピック









