当前位置:七道奇文章资讯数据防范MySQL防范
日期:2011-05-02 15:44:00  来源:本站整理

MySQL常用查询的例子[MySQL防范]

赞助商链接



  本文“MySQL常用查询的例子[MySQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
下面是一些学习若何用MySQL办理一些常见问题的例子.一些例子利用数据库表"shop",包含某个商人的每篇文章(物品号)的价钱.假定每个商人的每篇文章有一个单独的固定价钱,那么(物品,商人)是记录的主键.

你能这样成立例子数据库表:

CREATE TABLE shop (

article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,

dealer CHAR(20) DEFAULT '' NOT NULL,

price  DOUBLE(16,2) DEFAULT '0.00' NOT NULL,

PRIMARY KEY(article, dealer));

INSERT INTO shop VALUES

(1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),(3,'C',1.69),

(3,'D',1.25),(4,'D',19.95);

好了,例子数据是这样的:

SELECT * FROM shop

+---------+--------+-------+

| article | dealer | price |

+---------+--------+-------+

|0001 | A | 3.45 |

|0001 | B | 3.99 |

|0002 | A | 10.99 |

|0003 | B | 1.45 |

|0003 | C | 1.69 |

|0003 | D | 1.25 |

|0004 | D | 19.95 |

+---------+--------+-------+

3.1 列的最大值

"最大的物品号是什么?"

SELECT MAX(article) AS article FROM shop

+---------+

| article |

+---------+

|  4 |

+---------+

3.2 拥有某个列的最大值的行

"找出最贵的文章的编号、商人和价钱"

在ANSI-SQL中这很简单用一个子查询做到:

SELECT article, dealer, price

FROM  shop

WHERE price=(SELECT MAX(price) FROM shop)

在MySQL中(还没有子查询)就用2步做到:

用一个SELECT语句从表中得到最大值.

利用该值编出实际的查询:

SELECT article, dealer, price

FROM  shop

WHERE price=19.95

另一个办理筹划是按价钱降序排序全部行并用MySQL特定LIMIT子句只得到的第一行:

SELECT article, dealer, price

FROM  shop

ORDER BY price DESC

LIMIT 1

注意:假若有多个最贵的文章( 比方每个19.95),LIMIT办理筹划仅仅显示他们之一!

3.3 列的最大值:按组:只有值

"每篇文章的最高的价钱是什么?"

SELECT article, MAX(price) AS price

FROM  shop

GROUP BY article

+---------+-------+

| article | price |

+---------+-------+

|0001 | 3.99 |

|0002 | 10.99 |

|0003 | 1.69 |

|0004 | 19.95 |

+---------+-------+

3.4 拥有某个字段的组间最大值的行

"对每篇文章,找出有最贵的价钱的交易者."

在ANSI SQL中,我可以用这样一个子查询做到:

SELECT article, dealer, price

FROM  shop s1

WHERE price=(SELECT MAX(s2.price)

 FROM shop s2

 WHERE s1.article = s2.article)

在MySQL中,最好是分几步做到:

得到一个表(文章,maxprice).见3.4 拥有某个域的组间最大值的行.

对每篇文章,得到对应于存储最大价钱的行.

这可以很简单用一个暂时表做到:

CREATE TEMPORARY TABLE tmp (

article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,

price  DOUBLE(16,2) DEFAULT '0.00' NOT NULL);

LOCK TABLES article read;

INSERT INTO tmp SELECT article, MAX(price) FROM shop GROUP BY article;

SELECT article, dealer, price FROM shop, tmp

WHERE shop.article=tmp.articel AND shop.price=tmp.price;

UNLOCK TABLES;

DROP TABLE tmp;

假如你不利用一个TEMPORARY表,你也必须锁定"tmp"表.

"它能一个单个查询做到吗?"

是的,但是只有利用我称之为"MAX-CONCAT诡计"的一个相当低效的诡计:

SELECT article,

  SUBSTRING( MAX( CONCAT(LPAD(price,6,'0'),dealer) ), 7) AS dealer,

 0.00+LEFT( MAX( CONCAT(LPAD(price,6,'0'),dealer) ), 6) AS price

FROM  shop

GROUP BY article;

+---------+--------+-------+

| article | dealer | price |

+---------+--------+-------+

|0001 | B | 3.99 |

|0002 | A | 10.99 |

|0003 | C | 1.69 |

|0004 | D | 19.95 |

+---------+--------+-------+

最后例子当然能通过在客户程序中分割连结的列使它更有效一点.

3.5 利用外键

不需求外键联合2个表.

MySQL唯一不做的事情是CHECK以保证你利用的键确切在你正在引用表中存在,并且它不自动从有一个外键定义的表中删除行.假如你象普通那样利用你的键值,它将工作得很好!

CREATE TABLE persons (

id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,

name CHAR(60) NOT NULL,

PRIMARY KEY (id)

);

CREATE TABLE shirts (

id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,

style ENUM('t-shirt', 'polo', 'dress') NOT NULL,

color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,

owner SMALLINT UNSIGNED NOT NULL REFERENCES persons,

PRIMARY KEY (id)

);

INSERT INTO persons VALUES (NULL, 'Antonio Paz');

INSERT INTO shirts VALUES

(NULL, 'polo', 'blue', LAST_INSERT_ID()),

(NULL, 'dress', 'white', LAST_INSERT_ID()),

(NULL, 't-shirt', 'blue', LAST_INSERT_ID());

INSERT INTO persons VALUES (NULL, 'Lilliana Angelovska');

INSERT INTO shirts VALUES

(NULL, 'dress', 'orange', LAST_INSERT_ID()),

(NULL, 'polo', 'red', LAST_INSERT_ID()),

(NULL, 'dress', 'blue', LAST_INSERT_ID()),

(NULL, 't-shirt', 'white', LAST_INSERT_ID());

SELECT * FROM persons;

+----+---------------------+

| id | name|

+----+---------------------+

| 1 | Antonio Paz |

| 2 | Lilliana Angelovska |

+----+---------------------+

SELECT * FROM shirts;

+----+---------+--------+-------+

| id | style  | color | owner |

+----+---------+--------+-------+

| 1 | polo| blue  | 1 |

| 2 | dress  | white | 1 |

| 3 | t-shirt | blue  | 1 |

| 4 | dress  | orange | 2 |

| 5 | polo| red| 2 |

| 6 | dress  | blue  | 2 |

| 7 | t-shirt | white | 2 |

+----+---------+--------+-------+

SELECT s.* FROM persons p, shirts s

WHERE p.name LIKE 'Lilliana%'

  AND s.owner = p.id

  AND s.color <> 'white';

+----+-------+--------+-------+

| id | style | color | owner |

+----+-------+--------+-------+

| 4 | dress | orange | 2 |

| 5 | polo | red| 2 |

| 6 | dress | blue  | 2 |

+----+-------+--------+-------+   以上是“MySQL常用查询的例子[MySQL防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • Windows 搭配 IIS7 PHP MySQL 环境
  • mysql Out of memory (Needed 16777224 bytes)的错误办理
  • mysql提醒[Warning] Invalid (old?) table or database name问题的办理办法
  • mysql启用skip-name-resolve情势时呈现Warning的处理办法
  • mysql启用skip-name-resolve情势时呈现Warning的处理办法
  • MySQL Order By语法介绍
  • <b>MySQL ORDER BY 的实现解析</b>
  • mysql数据库插入速度和读取速度的调整记录
  • MySQL Order By索引优化办法
  • MySQL Order By用法分享
  • mysql #1062 –Duplicate entry ''1'' for key ''PRIMARY''
  • MySQL Order By Rand()效率解析
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .