Академический Документы
Профессиональный Документы
Культура Документы
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
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
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
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
http://oftob.com
http://oftob.com
http://oftob.com
27