mysql进阶1
在我们用php处理数据的时候总会遇到些比较麻烦的事情,比如:两个二维数组,一个装的是文章分类表内容,一个装的是文章列表,有关联字段,完全等值,要求在列表文章的时候同时能在标题的前面显示栏目名称,此时循环文章列表,在循环中再次循环栏目列表,对比关联字段,相同的就是这篇文章所在的栏目了,取其名称显示,这是php的做法,如果文章比较多,或者栏目较多,比如100篇文章有100个栏目,循环的数量将是一万次,这个时候呢其实可以用mysql自己进行计算的,这种做法是双表联查,基本语法如下:
Select表1.字段,表2.字段from表1,表2 where表1.关联字段=表2.关联字段,做个测试自行建如下表:
栏目表结构menu
tid int(10)
name varchar(32)
文章表结构article
wid int(10)
tid int(10)
title varchar(100)
content text
执行下面语句:
SELECT `menu`.`name`, `article`.`id`,`article`.`title`
FROM `article`,`menu`
WHERE `article`.`tid` = `menu`.`tid`
我只显示了栏目名称,文章的ID 和 标题 ,如果是取这个结果输出的话。。那就省事了。一次循环就可以了。
结果如上图。
在php运算量大的时候把一些运算交给mysql处理会大大减少php的工作量,数据量大的时候mysql会明显快点的,除了双表联查,mysql还提供了另外的一种做法叫做对照查询,语法如下:
SELECT表1.字段,表2字段
FROM表1
LEFT JOIN表2 ON表1.关联字段=表2.关联字段
在此使用了
LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。
在此使用的原理和上面的完全不同
原理是:对照查询,是以其中一表为基准。若是right则是表2为基准
而双表联查:则是按两张表的数据能关联上的时候。才视为符合查询条件。
如果用 inner join 那和双表联查的结果基本上一致了。
如果我们做的一个商城运营到一段时间后老板想知道有多少实际交易及每个用户的交易额,知道产生多少交易select count(*) from表名但是知道多少个用户交易过,就不好算了,每个用户又分别交易多少笔呢,这个时候可以使用分组功能,关键字为group by拿之前的表测试下我们想知道每个栏目下分别有多少篇文章相当于每个用户分别产生了多少笔交易,
select `tid`,count(*) from `article` GROUP BY `tid`
分组查询,相当于把数据库中的指定字段,相同的数据叠在一起。 这个时候使用count的话,返回的不是一共有多少组。而是每一组有多少条数据。
如果我们手上有几篇文章的id,如果我们手上有几篇文章的id,比如,我们现在手上有3 6 9三个id
按刚才的文章表。应该怎么查呢。 才能让它们出现在一个结果里。
估计有人会想到这个吧
select * from `article` where `wid`=3 or `wid`=6 or `wid` = 9
如果是这样的话,又会存在一个新的问题,对于 PHP 来说,要写很多 or 这个时候用in再合适不过,
in 语法格式
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
如果要查的值不是很多的话,用这种方式,是很省事的。 如果你要查的值,数据是成千上万级的。那就是另一回事。。子查询。
我先来做个假设,如果我们手上有几千个会员,我只想看前20个人的交易记录。 此时可以用对照查询,
我们查询用户表的语句大概会是这样子。
select * from `user` where `vip` = 1
比如交易表名叫shop购物表,两张表都有 username 字段对应。 我们就可以用子查询。把第一个查询结果假设为一张表, 假定为表 vip
如果用刚才的对照查询。语法会是这样
select * //返回所有字段
from `shop` //从交易表查
right join `vip` //以右边,也就是 VIP表为准
on `vip`.`username` = `shop`.`username`因为只要vip用户的交易记录。所以,左表shop的数据,有些是不要的。当然是以右表为准。
select * from `user` where `vip` = 1
右表是这条语句的查询结果
select *
from `shop`
right join
(select * from `user` where `vip` = 1) as `vip`
on `vip`.`username` = `shop`.`username`
(select * from `user` where `vip` = 1) as `vip`
把这条语句的查询结果,视为一张表,临时命名为 vip
这样一来,从 shop 表查到的交易记录。都会以 vip 这张“虚拟表”为准,选择性输出。
得到的结果,当然也就是所有 VIP 用户的交易记录了,这就是所谓的子查询,在一条查询语句里面,还有另一条查询语句。 当然,还有另外一种写法也可以做到,不用对照查询。
先准备好我们的子查询语句。所有 VIP 的用户名。
select `username` from `user` where `vip` = 1
select * from `shop` where `username` = '一个用户名' 这条语句,可以从交易表里,得到一个用户的所有交易记录, 如果两条语句合并起来呢。
select * from `shop` where `username` = (select `username` from `user` where `vip` = 1)
这是子查询的另一种用法。
这种做法,子查询返回的结果,必须只有一个字段。
select * from `article`
where `tid` in
(select `tid` from `menu` where `tid` > 1)
在数据量大的时候。。适当的选择这些“有点复杂”的 SQL 语句,让处理数据“更专业”的数据库把结果算好给我们。往往可以达到事半功倍的效果。
抱怨一句 想当好一个合格的程序员再怎么努力也不为过。。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.
