当前位置: 移动技术网 > IT编程>数据库>Mysql > 常用查询的例子——Frommysqlapi

常用查询的例子——Frommysqlapi

2019年04月19日  | 移动技术网IT编程  | 我要评论

常用查询的例子

下面是一些学习如何用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 |

+———+——–+——-+

8.3.1 列的最大值

“最大的物品号是什么?”

select max(article) as article from shop

+———+

| article |

+———+

| 4 |

+———+

8.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解决方案仅仅显示他们之一!

8.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 |

+———+——-+

8.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)。见8.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 |

+———+——–+——-+

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

8.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 |

+—-+——-+——–+——-+

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网