Você está na página 1de 27

http://oftob.

com

MySQL
MySQL .
MySQL.
: ..
( 0.1.ru-mysql 2012-02-14)

MySQL
, Creative Commons AttributionNonCommercial-ShareAlike (
) 3.0 .
oftob.com.
, ,
http://oftob.com.

http://oftob.com

.................................................................................................................................................. 3
SELECT. 1-10 ............................................................................................................................. 4
SELECT. 11-20 ........................................................................................................................... 6
SELECT. 21-30 ........................................................................................................................... 8
SELECT. 31-40 ......................................................................................................................... 11
SELECT. 41-50 ......................................................................................................................... 14
SELECT. 51-60 ......................................................................................................................... 17
UPDATE. 1-10 ......................................................................................................................... 19
""............................................................................................... 20
.................................................................................................. 23

http://oftob.com

MySQL .
SQL-. ,

SQL .

http://oftob.com

SELECT. 1-10
SELECT.
Q001. SQL- (*)
m_product, ,
m_product:
SELECT *
FROM m_product;
Q002. :
SELECT dt, product_id, amount
FROM m_income;
Q003. DISTINCT
:
SELECT DISTINCT product_id
FROM m_income;
Q004. ORDER BY ()
. ORDER
BY:
SELECT *
FROM m_income
ORDER BY price;
Q005. ASC ORDER BY
. DESC
ORDER BY .
, ASC, DESC , ASC (default):
SELECT *
FROM m_income
ORDER BY dt DESC , price;
Q006.
, .
WHERE. m_income
, amount 200:

http://oftob.com

SELECT *
FROM m_income
WHERE amount>200;
Q007. AND
(), OR () NOT ( ).
m_outcome , amount 20 price
10:
SELECT dt, product_id, amount, price
FROM m_outcome
WHERE amount=20 AND price>=10;
Q008.
INNER JOIN, LEFT JOIN, RIGHT JOIN. dt, product_id,
amount, price m_income title m_product.
m_income m_product
m_income.product_id m_product.id:
SELECT dt, product_id, title, amount, price
FROM m_income INNER JOIN m_product
ON m_income.product_id=m_product.id;
Q009. : 1)
( ' ); 2) "--".
.
12- 2011 . "2011-612":
SELECT dt, product_id, title, amount, price
FROM m_income INNER JOIN m_product
ON m_income.product_id=m_product.id
WHERE title='' And dt="2011-6-12";
Q010. BETWEEN
. ,
1- 30- 2011 :
SELECT *
FROM m_income INNER JOIN m_product
ON m_income.product_id=m_product.id
WHERE dt BETWEEN "2011-6-1" And "2011-6-30";

http://oftob.com

SELECT. 11-20
MySQL . - ,
. , WHERE.
.
Q011. m_product,
m_income:
SELECT *
FROM m_product
WHERE id IN (SELECT product_id FROM m_income);
Q012. m_product,
m_outcome:
SELECT *
FROM m_product
WHERE id NOT IN (SELECT product_id FROM m_outcome);
Q013. ,
m_income, m_outcome:
SELECT DISTINCT product_id, title
FROM m_income INNER JOIN m_product
ON m_income.product_id=m_product.id
WHERE product_id NOT IN (SELECT product_id FROM m_outcome);
Q014. m_category ,
:
SELECT DISTINCT title
FROM m_product
WHERE title LIKE '%';
Q015.
(alias).
= * ,
, 7 :
SELECT dt, product_id, amount, price, amount*price AS outcome_sum,
amount*price/100*7 AS profit
FROM m_outcome;
Q016. ,
:
6

http://oftob.com

SELECT dt, product_id, amount, price, amount*price AS outcome_sum,


amount*price*0.07 AS profit
FROM m_outcome;
Q017. INNER JOIN
. , ctgry_id,
m_income, m_category,
:
SELECT c.title, b.title, dt, amount, price, amount*price AS income_sum
FROM m_income AS a INNER JOIN m_product AS b ON a.product_id=b.id
INNER JOIN m_category AS c ON b.ctgry_id=c.id
ORDER BY c.title, b.title;
Q018. SUM - , COUNT - , AVG
, MAX , MIN
. ,
.
amount price SUM:
SELECT SUM(amount*price) AS Total_Sum
FROM m_income;
Q019. :
SELECT Sum(amount) AS Amount_Sum, AVG(amount) AS Amount_AVG,
MAX(amount) AS Amount_Max, Min(amount) AS Amount_Min,
Count(*) AS Total_Number
FROM m_income;
Q020. 1,
2011 :
SELECT Sum(amount*price) AS income_sum
FROM m_income
WHERE product_id=1 AND dt BETWEEN "2011-6-1" AND "2011-6-30";

http://oftob.com

SELECT. 21-30
Q021. ,
4 6:
SELECT Sum(amount*price) as outcome_sum
FROM m_outcome
WHERE product_id=4 OR product_id=6;
Q022. 12 2011
, 4 6:
SELECT Sum(amount*price) AS outcome_sum
FROM m_outcome
WHERE (product_id=4 OR product_id=6) AND dt="2011-6-12";
Q023. .
" ".
: m_income, m_product
m_category, :
- m_income;
- m_product;
- title m_category.
:
- " " m_category
;
- m_income m_product
;
- ( = *) ,
, .
:
SELECT Sum(amount*price) AS income_sum
FROM m_product AS a INNER JOIN m_income AS b ON a.id=b.product_id
WHERE ctgry_id = (SELECT id FROM m_category WHERE title=' ');
Q024.
" " :
- m_income, product_id,
m_category, ;
- , " ";
- = *.

http://oftob.com

:
SELECT Sum(amount*price) AS income_sum
FROM m_product AS a
INNER JOIN m_income AS b ON a.id=b.product_id
INNER JOIN m_category AS c ON a.ctgry_id=c.id
WHERE c.title=' ';
Q025.
:
SELECT COUNT(DISTINCT product_id) AS product_cnt
FROM m_outcome AS t;
Q026. GROUP BY .
,
- . ,
. ,
, :
SELECT title, SUM(amount*price) AS outcome_sum
FROM m_product AS a INNER JOIN m_outcome AS b
ON a.id=b.product_id
GROUP BY title;
Q027. . ,
, ,
, . ROUND
(
):
SELECT c.title, SUM(amount*price) AS outcome_sum,
ROUND(AVG(amount*price),2) AS outcome_sum_avg
FROM m_product AS a
INNER JOIN m_outcome AS b ON a.id=b.product_id
INNER JOIN m_category AS c ON a.ctgry_id=c.id
GROUP BY c.title;
Q028.
,
500:
SELECT product_id, SUM(amount) AS amount_sum,
ROUND(AVG(amount),2) AS amount_avg
FROM m_income

http://oftob.com

GROUP BY product_id
HAVING Sum(amount)>=500;
Q029.
, 2011 .
500, :
SELECT title, SUM(amount*price) AS income_sum
FROM m_income a INNER JOIN m_product b ON a.product_id=b.id
WHERE dt BETWEEN "2011-4-1" AND "2011-6-30"
GROUP BY title
HAVING SUM(amount*price)>=1000;
Q030.
; . ,
, . ,
100 15 ,
100*15=150 .
m_income m_outcome:
SELECT *
FROM m_income, m_outcome;

10

http://oftob.com

SELECT. 31-40
Q031. .
:
SELECT supplier_id, product_id, SUM(amount) AS amount_sum,
SUM(amount*price) AS income_sum
FROM m_income AS a INNER JOIN m_product AS b ON a.product_id=b.id
GROUP BY supplier_id, product_id;
Q032. .
, :
SELECT supplier_id, product_id, SUM(amount) AS amount_sum,
SUM(amount*price) AS outcome_sum
FROM m_outcome AS a INNER JOIN m_product AS b ON a.product_id=b.id
GROUP BY supplier_id, product_id;
Q033. (q031 q032)
. LEFT JOIN .

. , -
, , outcome_sum
. ,
.
SQL :
SELECT *
FROM
(SELECT supplier_id, product_id, SUM(amount) AS amount_sum,
SUM(amount*price) AS income_sum
FROM m_income AS a INNER JOIN m_product AS b
ON a.product_id=b.id GROUP BY supplier_id, product_id) AS a
LEFT JOIN
(SELECT supplier_id, product_id, SUM(amount) AS amount_sum,
SUM(amount*price) AS outcome_sum
FROM m_outcome AS a INNER JOIN m_product AS b
ON a.product_id=b.id GROUP BY supplier_id, product_id) AS b
ON (a.product_id=b.product_id) AND (a.supplier_id=b.supplier_id);
Q034. (q031 q032)
. RIGTH JOIN .

.

. , - ,
11

http://oftob.com

, income_sum .
,
, :
SELECT *
FROM
(SELECT supplier_id, product_id, SUM(amount) AS amount_sum,
SUM(amount*price) AS income_sum
FROM m_income AS a INNER JOIN m_product AS b ON a.product_id=b.id
GROUP BY supplier_id, product_id) AS a
RIGHT JOIN
(SELECT supplier_id, product_id, SUM(amount) AS amount_sum,
SUM(amount*price) AS outcome_sum
FROM m_outcome AS a INNER JOIN m_product AS b ON a.product_id=b.id
GROUP BY supplier_id, product_id) AS b
ON (a.supplier_id=b.supplier_id) AND (a.product_id=b.product_id);
Q035. .
m_income m_outcome,
m_income
m_outcome:
SELECT product_id, SUM(in_amount) AS income_amount,
SUM(out_amount) AS outcome_amount
FROM
(SELECT product_id, amount AS in_amount, 0 AS out_amount
FROM m_income
UNION ALL
SELECT product_id, 0 AS in_amount, amount AS out_amount
FROM m_outcome) AS t
GROUP BY product_id;
Q036. EXISTS TRUE,
. EXISTS FALSE,
, .
, m_income, m_outcome:
SELECT DISTINCT product_id
FROM m_income AS a
WHERE EXISTS(SELECT product_id FROM m_outcome AS b
WHERE b.product_id=a.product_id);
Q037. , m_income,
m_outcome:

12

http://oftob.com

SELECT DISTINCT product_id


FROM m_income AS a
WHERE product_id IN (SELECT product_id FROM m_outcome)
Q038. , m_income,
m_outcome:
SELECT DISTINCT product_id
FROM m_income AS a
WHERE NOT EXISTS(SELECT product_id FROM m_outcome AS b
WHERE b.product_id=a.product_id);
Q039. ,
. . .
, . ,
, ,
:
SELECT product_id, SUM(amount*price) AS amount_sum
FROM m_outcome
GROUP BY product_id
HAVING SUM(amount*price) = (SELECT MAX(s_amount)
FROM (SELECT SUM(amount*price) AS s_amount FROM m_outcome GROUP BY product_id)t);
Q040. IF ( ) MySQL

(TRUE FALSE). ,
500. ,
500, :
SELECT dt, product_id, amount,
IF(amount<500,"","") AS mark
FROM m_income;

13

http://oftob.com

SELECT. 41-50
Q041. , IF ,
CASE. CASE ( )

. ,
500. ,
500, :
SELECT dt, product_id, amount,
CASE WHEN amount<500 THEN "" ELSE "" END AS mark
FROM m_income;
Q042.
300, . , amount<300
, 500.
500, .
:
SELECT dt, product_id, amount,
IF(amount<300,"",
IF(amount<1000,"","")) AS mark
FROM m_income;
Q043.
300, . , amount<300
, 500.
500, .
:
SELECT dt, product_id, amount,
CASE
WHEN amount<300 THEN ""
WHEN amount<1000 THEN ""
ELSE ""
END AS mark
FROM m_income;
Q044. : ( 150),
(150 300), (300 ). ,
:
SELECT Category, SUM(outcome_sum) AS Ctgry_Total
FROM (SELECT amount*price AS outcome_sum,
IF(amount*price<150,"",
IF(amount*price<300,"","")) AS Category
14

http://oftob.com

FROM m_outcome) AS t
GROUP BY Category;
Q045. DateAdd ,
. :
1) dt 30 dt_plus_30d;
2) dt 1 dt_plus_1m:
SELECT dt, DATE_ADD(dt, INTERVAL 30 DAY) AS dt_plus_30d,
DATE_ADD(dt, INTERVAL 1 MONTH) AS dt_plus_1m
FROM m_income;
Q046. DateDiff
. dt
:
SELECT dt, DATEDIFF(NOW(),dt) AS last_day
FROM m_income;
Q047. (
m_income) DateDiff
( m_product):
SELECT a.id, product_id, dt, lifedays, DATEDIFF(NOW(),dt) AS last_day
FROM m_income AS a INNER JOIN m_product AS b
ON a.product_id=b.id;
Q048. ,
:
SELECT a.id, product_id, dt, lifedays, DATEDIFF(NOW(),dt) AS last_days,
IF(DATEDIFF(NOW(),dt)>lifedays,"","") AS date_expire
FROM m_income a INNER JOIN m_product b
ON a.product_id=b.id;
Q049.
:
SELECT
PERIOD_DIFF(EXTRACT(YEAR_MONTH FROM NOW()), EXTRACT(YEAR_MONTH FROM dt)) AS
months
FROM m_income;
Q050.
2011 :

15

http://oftob.com

SELECT kvartal, SUM(outcome_sum) AS Total


FROM (SELECT amount*price AS outcome_sum, month(dt) AS m,
CASE WHEN month(dt)<4 THEN 1
WHEN month(dt)<7 THEN 2
WHEN month(dt)<10 THEN 3
ELSE 4 END AS kvartal
FROM m_income WHERE year(dt)=2011) AS t
GROUP BY kvartal

16

http://oftob.com

SELECT. 51-60
Q051. SQL- ,
,
:
SELECT product_id, SUM(in_sum) AS income_sum, SUM(out_sum) AS outcome_sum
FROM (SELECT product_id, amount*price as in_sum, 0 as out_sum
from m_income
UNION ALL
SELECT product_id, 0 as in_sum, amount*price as out_sum
from m_outcome) AS t
GROUP BY product_id
HAVING SUM(in_sum)<SUM(out_sum);
Q052. , , -.
. H, PHP.
SQL. ,
m_income
ID:
SELECT COUNT(*) as N, b.id, b.product_id, b.amount, b.price
FROM m_income a INNER JOIN m_income b ON a.id <= b.id
GROUP BY b.id, b.product_id, b.amount, b.price;
Q053. .
LIMIT:
SELECT product_id, sum(amount*price) AS summa
FROM m_outcome
GROUP BY product_id
ORDER BY sum(amount*price) DESC
LIMIT 0, 5;
Q054. ,
:
SELECT COUNT(*) AS N, b.product_id, b.summa
FROM
(SELECT product_id, sum(amount*price) AS summa, sum(amount*price)*10000000+product_id
AS id
FROM m_outcome GROUP BY product_id) AS a
INNER JOIN
(SELECT product_id, sum(amount*price) AS summa, sum(amount*price)*10000000+product_id
AS id
FROM m_outcome GROUP BY product_id) AS b
17

http://oftob.com

ON a.id>=b.id
GROUP BY b.product_id, b.summa
HAVING COUNT(*)<=5
ORDER BY COUNT(*);

18

http://oftob.com

UPDATE. 1-10
U001. SQL- (UPDATE) 10%
3 m_income:
UPDATE m_income
SET price = price*1.1
WHERE product_id=3;
U002. SQL- m_income
22 , "":
UPDATE m_income
SET amount = amount+22
WHERE product_id IN (SELECT id FROM m_product WHERE title LIKE "%");
U003. SQL- m_outcome 2
, "":
UPDATE m_outcome SET price = price*0.98
WHERE product_id IN
(SELECT a.id FROM m_product a INNER JOIN m_supplier b
ON a.supplier_id=b.id WHERE b.title=' ""');

19

http://oftob.com

""


m_category -

id

title

m_income -

id

dt

product_id

20

http://oftob.com

amount

price

m_outcome -

id

dt

product_id

amount

price

m_product - ,

id

title

supplier_id

ctgry_id

unit

lifedays

m_supplier - ;

id

21

http://oftob.com

title

phone

address

m_unit - ;

title

description

,
:
- MySQL 5 .
- phpMyAdmin
, () .

22

http://oftob.com


MySQL ,
:
--- Table structure for table `m_category`
-CREATE TABLE IF NOT EXISTS `m_category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--- Dumping data for table `m_category`
-INSERT INTO `m_category` (`id`, `title`)
VALUES
(1, ' '),
(2, ', '),
(3, ''),
(4, ' '),
(5, ' ');
--- Table structure for table `m_income`
-CREATE TABLE IF NOT EXISTS `m_income` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` date NOT NULL,
`product_id` int(11) NOT NULL,
`amount` int(11) NOT NULL,
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--- Dumping data for table `m_income`
-INSERT INTO `m_income` (`id`, `dt`, `product_id`, `amount`, `price`)
VALUES
23

http://oftob.com

(65, '2011-01-01', 1, 1000, 12.50),


(66, '2011-02-02', 3, 200, 4.97),
(67, '2011-03-12', 4, 164, 10.00),
(68, '2011-06-11', 7, 200, 20.00),
(69, '2011-06-11', 5, 299, 12.35),
(70, '2011-06-12', 1, 300, 12.40),
(71, '2011-06-12', 7, 100, 19.50),
(72, '2011-06-12', 2, 100, 15.75),
(73, '2011-06-12', 5, 3064, 11.80),
(74, '2011-06-12', 6, 264, 17.90),
(75, '2011-06-12', 3, 150, 4.97),
(76, '2011-06-13', 3, 150, 4.97),
(77, '2011-06-13', 10, 100, 4.20),
(78, '2011-06-14', 9, 100, 2.95),
(79, '2011-06-14', 10, 130, 4.20),
(80, '2011-06-14', 9, 110, 2.95),
(81, '2011-06-18', 8, 155, 3.10),
(82, '2011-06-19', 8, 450, 3.10),
(83, '2011-06-11', 5, 599, 12.35);
--- Table structure for table `m_income_backup`
-CREATE TABLE IF NOT EXISTS `m_income_backup` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` date NOT NULL,
`product_id` int(11) NOT NULL,
`amount` int(11) NOT NULL,
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--- Dumping data for table `m_income_backup`
-INSERT INTO `m_income_backup` (`id`, `dt`, `product_id`, `amount`, `price`)
VALUES
(65, '2011-01-01', 1, 1000, 12.50),
(66, '2011-02-02', 3, 200, 3.74),
(67, '2011-03-12', 4, 142, 10.00),
(68, '2011-06-11', 7, 200, 20.00),
(69, '2011-06-11', 5, 277, 12.35),
(70, '2011-06-12', 1, 300, 12.40),
(71, '2011-06-12', 7, 100, 19.50),
24

http://oftob.com

(72, '2011-06-12', 2, 100, 15.75),


(73, '2011-06-12', 5, 3042, 11.80),
(74, '2011-06-12', 6, 242, 17.90),
(75, '2011-06-12', 3, 150, 3.74),
(76, '2011-06-13', 3, 150, 3.74),
(77, '2011-06-13', 10, 100, 4.20),
(78, '2011-06-14', 9, 100, 2.95),
(79, '2011-06-14', 10, 130, 4.20),
(80, '2011-06-14', 9, 110, 2.95),
(81, '2011-06-18', 8, 155, 3.10),
(82, '2011-06-19', 8, 450, 3.10),
(83, '2011-06-11', 5, 577, 12.35);
--- Table structure for table `m_outcome`
-CREATE TABLE IF NOT EXISTS `m_outcome` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` date NOT NULL,
`product_id` int(11) NOT NULL,
`amount` int(11) NOT NULL,
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--- Dumping data for table `m_outcome`
-INSERT INTO `m_outcome` (`id`, `dt`, `product_id`, `amount`, `price`)
VALUES
(26, '2011-06-12', 1, 10, 12.70),
(27, '2011-06-12', 3, 23, 3.49),
(28, '2011-06-12', 3, 34, 3.58),
(29, '2011-06-12', 4, 30, 10.35),
(30, '2011-06-12', 7, 20, 20.40),
(31, '2011-06-12', 4, 1, 11.29),
(32, '2011-06-12', 5, 20, 13.00),
(33, '2011-06-13', 6, 25, 18.20),
(34, '2011-06-18', 8, 20, 3.40),
(35, '2011-07-01', 11, 4, 5.50),
(36, '2011-06-12', 4, 23, 10.54),
(37, '2011-12-12', 6, 15, 22.00),
(38, '2011-12-12', 4, 25, 17.13),
(39, '2011-12-12', 2, 13, 30.00),
25

http://oftob.com

(40, '2011-12-13', 2, 1, 18.00);


--- Table structure for table `m_outcome_backup`
-CREATE TABLE IF NOT EXISTS `m_outcome_backup` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` date NOT NULL,
`product_id` int(11) NOT NULL,
`amount` int(11) NOT NULL,
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--- Dumping data for table `m_outcome_backup`
-INSERT INTO `m_outcome_backup` (`id`, `dt`, `product_id`, `amount`, `price`)
VALUES
(26, '2011-06-12', 1, 10, 12.70),
(27, '2011-06-12', 3, 23, 3.70),
(28, '2011-06-12', 3, 34, 3.80),
(29, '2011-06-12', 4, 30, 11.00),
(30, '2011-06-12', 7, 20, 20.40),
(31, '2011-06-12', 4, 1, 12.00),
(32, '2011-06-12', 5, 20, 13.00),
(33, '2011-06-13', 6, 25, 18.20),
(34, '2011-06-18', 8, 20, 3.40),
(35, '2011-07-01', 11, 4, 5.50),
(36, '2011-06-12', 4, 23, 11.20),
(37, '2011-12-12', 6, 15, 22.00),
(38, '2011-12-12', 4, 25, 18.20),
(39, '2011-12-12', 2, 13, 30.00),
(40, '2011-12-13', 2, 1, 18.00);
--- Table structure for table `m_product`
-CREATE TABLE IF NOT EXISTS `m_product` (
`id` int(11) NOT NULL,
`title` varchar(30) NOT NULL,
`supplier_id` int(11) NOT NULL,
`ctgry_id` int(11) NOT NULL,
26

http://oftob.com

`unit` varchar(255) NOT NULL,


`lifedays` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--- Dumping data for table `m_product`
-INSERT INTO `m_product` (`id`, `title`, `supplier_id`, `ctgry_id`, `unit`, `lifedays`)
VALUES
(1, ' ', 1, 4, '', 90),
(2, ' ', 3, 4, '', 90),
(3, '', 2, 1, '', 2),
(4, ' , ', 2, 2, '', 30),
(5, ' , ', 3, 2, '', 90),
(6, ' , ', 1, 2, '', 10),
(7, ' ', 1, 3, '', 5),
(8, ', ', 1, 1, '', 2),
(9, '', 2, 5, '', 1),
(10, '', 3, 5, '', 1),
(11, '', 1, 1, '', 2);
--- Table structure for table `m_supplier`
-CREATE TABLE IF NOT EXISTS `m_supplier` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(30) NOT NULL,
`phone` varchar(20) NOT NULL,
`address` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--- Dumping data for table `m_supplier`
-INSERT INTO `m_supplier` (`id`, `title`, `phone`, `address`)
VALUES
(1, ' ""', '', ''),
(2, ' ""', '', ''),
(3, ' ..', '', '');

27

Você também pode gostar