´ë¿ë·® DB °ü¸® (MySQL) :: ºÐ·ù¾øÀ½
MySQL ·Ñ¹éÀÌ ¾ÈµÈ´Ù~~(½ºÅ丮Áö ¿£Áø¿¡ µû¶ó ´Ù¸£´Ù³×;;)
SELECT
°¡´ÉÇÑ ¸í½ÃÀûÀ¸·Î Äõ¸®Çضó~~
LIMITÀ» »ç¿ëÇؼ ½ºÅ©·ÑÇضó
mysql> SELECT * FROM City ORDER BY Id LIMIT 0,2;
+----+----------+---------+----------+------------+
| Id | Name | Country | District | Population |
+----+----------+---------+----------+------------+
| 1 | Kabul | AFG | Kabol | 1780000 |
| 2 | Qandahar | AFG | Qandahar | 237500 |
+----+----------+---------+----------+------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM City ORDER BY Id LIMIT 2,2;
+----+----------------+---------+----------+------------+
| Id | Name | Country | District | Population |
+----+----------------+---------+----------+------------+
| 3 | Herat | AFG | Herat | 186800 |
| 4 | Mazar-e-Sharif | AFG | Balkh | 127800 |
+----+----------------+---------+----------+------------+
2 rows in set (0.00 sec)
ÃÑ ·¹ÄÚµå °³¼ö ¾Ë±â
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM City ORDERBY BY id LIMIT 0, 2;
mysql> SELECT FOUND_ROWS() AS TOTAL;
+-------+
| TOTAL |
+-------+
| 4079 |
+-------+
1 row in set (0.00 sec)
LIMITÀº ³×Æ®¿÷À¸·Î °¡Àú¿À´Â ¾ç¸¸ Á¶ÀýÇÑ´Ù.
¾îÂ¥ÇÇ Àüü ¿¬»êÀ» Çϱ⶧¹®¿¡ LIMITÀ» ½áµµ Àüü Row¸¦ ¾Æ´Âµ¥´Â
ÁöÀå¾ø°í ´õ ºü¸¥ ÀåÁ¡ÀÌ ÀÖ´Ù.
DISTINCT : OracleÇÒ¶§´Â ¼º´É¶§¹®¿¡ ¾²Áö ¸»¶ó°í Çß¾ú´Âµ¥-_-;;;
AS : Ä÷³¸í ÁöÁ¤
mysql> SELECT Name, Population,
-> Population * 1.20 AS NewPop
-> FROM Country LIMIT 5;
+----------------------+------------+-------------+
| Name | Population | NewPop |
+----------------------+------------+-------------+
| Afghanistan | 22720000 | 27264000.00 |
| Netherlands | 15864000 | 19036800.00 |
| Netherlands Antilles | 217000 | 260400.00 |
| Albania | 3401200 | 4081440.00 |
| Algeria | 31471000 | 37765200.00 |
+----------------------+------------+-------------+
5 rows in set (0.00 sec)
WHERE
¿ÍÀϵåÄ«µå
'_' : ÀÓÀÇÀÇ Ä³¸¯ÅÍ Çϳª (?)
'%' : ÀÓÀÇÀÇ ¹®ÀÚ¿ (*)
ORDER BY & GROUP BY
GROUP BY´Â ORDER BY ¾Õ¿¡ ¿Â´Ù
µÑ ´Ù AS·Î Á¤ÀÇµÈ Çʵ带 »ç¿ëÇÒ ¼ö ÀÖÀ½
µÑ ´Ù ¿©·¯ °³ÀÇ Çʵ带 »ç¿ëÇÒ ¼ö ÀÖÀ½
GROUP BY¿Í ORDER BY¿¡ ¼·Î ´Ù¸¥ Çʵ带 »ç¿ë °¡´É
GROUP BY¸¦ »ç¿ëÇÒ °æ¿ì
- ¹Ýµå½Ã ÇØ´ç Çʵ带 SELECTÇØ¾ß ÇÑ´Ù
- ´õ Ãß·Á³»±â À§ÇÏ¿© HAVING Á¶°ÇÀýÀ» »ç¿ëÇÒ ¼ö ÀÖ´Ù
ORDER BY¸¦ »ç¿ëÇÒ °æ¿ì
- ¿ªÂ÷¼ø Á¤·ÄÀ» ¿øÇÏ´Â °æ¿ì DESC¸¦ »ç¿ë
- Ä®·³ ¹øÈ£·Î ±¸ºÐ °¡´É
- ORDER BY¸¦ À§ÇØ ¹Ýµå½Ã ÇØ´ç Çʵ带 SELECTÇÒ ÇÊ¿ä´Â ¾øÀ½
SUM, AVG, COUNT, MAX, MIN, STD(Ç¥ÁØÆíÂ÷)
GROUP_CONCAT() : ±×·ì¿¡¼ ¿¬°áµÈ ¹®ÀÚÇü °á°ú°ªÀ» ¾ò´Â´Ù.
mysql> SELECT continent, GROUP_CONCAT(distinct region)
-> FROM country GROUP BY continent;
+---------------+-----------------------------------------------------+
| continent | GROUP_CONCAT(distinct region) |
+---------------+-----------------------------------------------------+
| Asia | Eastern Asia,Middle East,Southeast Asia,Southern a¡¦ |
| Europe | Southern Europe,Baltic Countries,Eastern Europe,Nor¡¦|
| North America | Central America,Caribbean,North America |
| Africa | Central Africa,Eastern Africa,Western Africa,Northe¡¦|
| Oceania | Australia and New Zealand,Melanesia,Polynesia,Micr¡¦ |
| Antarctica | Antarctica |
| South America | South America |
+---------------+-----------------------------------------------------+
7 rows in set (0.00 sec)
GROUP BY ¡¦ WITH ROLLUPÀº GROUP BY¿¡ »ç¿ëµÈ
Ä®·³ÀÇ µ¥ÀÌÅ͸¦ ÇÕ°èÇÏ¿© ¿ä¾àµÈ µ¥ÀÌÅ͸¦ ¸¸µç´Ù.
- ¿ä¾à row´Â GROUP BYÀÇ °¡Àå ÁÂÃø Ä®·³ÀÌ ¹Ù²ð ¶§ ¸¶´Ù
»ý¼ºµÈ´Ù
- NULLÀº ¾î¶°ÇÑ Ä®·³ÀÌ ¿ä¾à row¸¦ ¸¸µå´Âµ¥ »ç¿ëµÇ¾ú´ÂÁö¸¦
Ç¥½ÃÇϱâ À§ÇØ »ç¿ëµÈ´Ù
- ¿ä¾à row´Â Äõ¸®ÀÇ ¼öÇàÀÌ ¸ðµÎ ¿Ï·á µÈ ´ÙÀ½¿¡ Ãß°¡µÇ¹Ç·Î
HAVING ¶Ç´Â À¯»çÇÑ ¹æ¹ýÀ¸·Î ÂüÁ¶ÇÒ ¼ö ¾ø´Ù
mysql> SELECT continent, name, SUM(Population)
-> FROM Country
-> GROUP BY continent, name
-> WITH ROLLUP;
+---------------+------------------------+-----------------+
| continent | name | SUM(Population) |
+---------------+------------------------+-----------------+
| Asia | Afghanistan | 22720000 |
| Asia | Armenia | 3520000 |
| Asia | Azerbaijan | 7734000 |
| Asia | Bahrain | 617000 |
| Asia | Bangladesh | 129155000 |
| ¡¦ | ¡¦ | ¡¦ |
| South America | Venezuela | 24170000 |
| South America | NULL | 345780000 |
| NULL | NULL | 6078749450 |
+---------------+------------------------+-----------------+
INSERT
mysql> INSERT INTO Country (Code, Name, Continent)
-> VALUES(¡®FIN¡¯, ¡®Finland¡¯, ¡®Europe¡¯),
-> (¡®SWE¡¯, ¡®Sweden¡¯, ¡®Europe¡¯);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
¸ðµç Çʵ忡 °ªÀ» ÀÔ·ÂÇϸé ÇÊµå ¸®½ºÆ®´Â ÇÊ¿ä ¾ø´Ù.
¿©·¯°³ÀÇ ·¹Äڵ带 Çѹø¿¡ ÀÔ·ÂÇÒ ¼ö ÀÖ´Ù.
AUTO_INCREMENT Çʵ忡´Â NULLÀ̳ª 0°ªÀ» ÁÖ¸é ¾ÈµÈ´Ù.
INSERT INTO SELECT
mysql> INSERT INTO CountryCopy
-> SELECT * FROM Country;
Query OK, 239 rows affected (0.03 sec)
Records: 239 Duplicates: 0 Warnings: 0
CREATE TABLE ¡¦ SELECT¿Í ºñ½ÁÇϳª Å×À̺í Á¤ÀǸ¦ ¸¸µéÁö ¾Ê´Â´Ù
SELECT´Â SELECT±¸¹®¿¡ ¸Â´Â °ÍÀÌ¸é ¾î¶°ÇÑ °ÍÀ̵ç
°¡´ÉÇϸç Ä®·³ ŸÀÔÀº ÇÊ¿äÇÏ´Ù¸é ÀÚµ¿À¸·Î º¯È¯µÈ´Ù
UPDATE
mysql> UPDATE CountryCopy
-> SET HeadOfState = 'Wizard'
-> WHERE Code = 'OZ';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
4.0.2 ¹öÀü ÀÌÈÄ·Î JOINÁ¶°ÇÀ» ÁÖ¾î ÇѲ¨¹ø¿¡ ¿©·¯ Å×À̺íÀ» ¾÷µ¥ÀÌÆ® ÇÒ ¼ö ÀÖ´Ù
Çѹø¿¡ ¿©·¯ Çʵ带 SET¿¡ ¼³Á¤ÇÒ ¼ö ÀÖ´Ù
SET ÇÒ ¶§ °ªÀÌ ¾Æ´Ñ Çʵ尡 Æ÷ÇÔµÈ Ç¥ÇöÀ» »ç¿ëÇÒ ¼ö ÀÖÀ¸¸ç
ÇÊµå ±× ÀÚü¸¦ ÂüÁ¶Çϴ ǥÇöÀ» »ç¿ëÇÒ ¼ö ÀÖ´Ù
LIMITÀ̳ª ORDER BY¸¦ »ç¿ëÇÏ¿© ¿µÇâÀ» ¹Þ°Ô µÉ row¸¦ Á¶Á¤ÇÒ ¼ö ÀÖ´Ù
DELETE
mysql> DELETE FROM CountryCopy
-> WHERE code = 'VAL';
Query OK, 0 rows affected (0.00 sec)
4.0¹öÀü ÀÌÈÄ·Î JOINÀ» »ç¿ëÇÏ¿© ¿©·¯ °³ÀÇ Å×ÀÌºí¿¡¼ Çѹø¿¡ DELETEÇÒ ¼ö ÀÖ´Ù
UPDATE¿Í ¸¶Âù°¡Áö·Î LIMITÀ» »ç¿ëÇÒ ¼ö ÀÖ´Ù
4.0¹öÀü ÀÌÈÄ·Î ORDER BY¿ª½Ã »ç¿ëÇÒ ¼ö ÀÖ´Ù
TRUNCATE TABLE
mysql> DELETE FROM CountryCopy
-> WHERE code = 'VAL';
Query OK, 0 rows affected (0.00 sec)
InnoDB Å×ÀÌºí¿¡¼´Â TRUNCATE TABLEÀº Á¤È®È÷
DELETE FROMÀ» WHEREÀý ¾øÀÌ »ç¿ëÇÑ°Í°ú ¶È°°´Ù
±× ¿Ü ½ºÅ丮Áö ¿£Áø¿¡¼´Â TRUNCATE TABLEÀº
¸î °¡Áö ºÎºÐ¿¡¼ DELETE¿Í ´Ù¸£´Ù
- Å×À̺íÀÌ µå·ÓµÇ°í ´Ù½Ã »ý¼ºµÊ
- Çѹø ¼öÇàµÇ°í ³ª¸é µÇµ¹¸± ¼ö ¾øÀ½
- MyISAMÀÇ °æ¿ì¿¡µµ AUTO_INCREMENT°ªÀÌ ¸®¼ÂµÊ
- Å×À̺íÀ» ÃʱâÈÇÏ´Â °¡Àå ºü¸¥ ¹æ¹ý
- ÆÄÀÏÀÌ »èÁ¦µÇ±â ¶§¹®¿¡ ¹®Á¦°¡ ¹ß»ýÇÑ Å×ÀÌºí¿¡µµ Àß ÀÛµ¿ÇÔ
MySQL È®Àå
REPLACE´Â INSERT ¶Ç´Â DELETE + INSERTÀÌ´Ù
Å°ÀÇ À¯Àϼº¿¡ ÀúÃ赃 °æ¿ì »èÁ¦¸¦ ¸ÕÀú ÇÏ°Ô µÈ´Ù
Ç¥ÁØ SQLÀÌ ¾Æ´Ñ MySQL È®Àå SQLÀÌ´Ù
ÄÚµù ½Ã°£°ú ÀÀ´ä½Ã°£À» Çâ»ó½ÃŲ´Ù
µ¥ÀÌÅͺ£À̽º Å×À̺íÀÇ ÇöÀç row¸¦ ¿ÏÀüÈ÷ ¹Ù²Ù°íÀÚ
ÇÒ °æ¿ì Ưº°È÷ À¯¿ëÇÏ´Ù
REPLACE´Â Àý´ë ÇöÀç ·¹Äڵ带 ¾÷µ¥ÀÌÆ® ÇÏ´Â °ÍÀÌ ¾Æ´Ï´Ù.
ºüÁø Çʵå´Â ±âº»°ªÀ» °¡Áö°Ô µÈ´Ù
¸¸¾à ·¹Äڵ尡 ¿©·¯ °³ÀÇ À¯´ÏÅ© Å°¿Í Ãæµ¹ÇÑ´Ù¸é ÇØ´ç
·¹ÄÚµå´Â ¸ðµÎ »èÁ¦µÇ°Ô µÈ´Ù
mysql> ALTER TABLE CountryCopy ADD UNIQUE (Code);
Query OK, 239 rows affected (0.42 sec)
Records: 239 Duplicates: 0 Warnings: 0
mysql> REPLACE INTO CountryCopy
-> (Code, Name, Continent)
-> VALUE('OZ', 'Country of Oz', 'Oceania');
Query OK, 1 row affected (0.03 sec)
mysql> REPLACE INTO CountryCopy
-> (Code, Name, Continent)
-> VALUES('OZ', 'Republic of Oz', 'Europe');
Query OK, 2 rows affected (0.02 sec)
INSERT ¡¦ ON DUPLICATE KEY UPDATE
ON DUPLICATE KEY UPDATE´Â À¯´ÏÅ© À妽º ¶Ç´Â ÇÁ¶óÀ̸Ӹ® Å°¿Í
Áߺ¹µÇ´Â °æ¿ì ÀμƮ ´ë½Å ¾÷µ¥ÀÌÆ®¸¦ ¼öÇàÇÏ°Ô µÈ´Ù
REPLACE¿Í ´Ù¸¥ Á¡Àº ÀÌÀü °ªÀÌ À¯ÁöµÇ´Â »óÅ¿¡¼ ÁöÁ¤µÈ
Ä®·³ÀÇ °ª¸¸ÀÌ º¯°æµÈ´Ù´Â °ÍÀÌ´Ù
mysql> INSERT INTO CountryCopy (Code, Name)
-> VALUES ('OZ', 'Republic of Oz')
-> ON DUPLICATE KEY UPDATE
-> Code = 'OZ', Name = 'Republic of Oz';
Query OK, 2 rows affected (0.03 sec)
ÇÔ¼ö
IN : ¿À¶óŬÇÏ°í °°´Ù.
mysql> SELECT Code, Name, Population
-> FROM Country
-> WHERE Code IN('FIN', 'SWE', 'NOR', 'DNK', 'ISL');
+------+---------+------------+
| Code | Name | Population |
+------+---------+------------+
| DNK | Denmark | 5330000 |
| FIN | Finland | 5171300 |
| ISL | Iceland | 279000 |
| NOR | Norway | 4478500 |
| SWE | Sweden | 8861400 |
+------+---------+------------+
5 rows in set (0.00 sec)
IF: ±×³É IF
IF(Boolean_Test, ValueIfTrue, ValueIfFalse)
Boolean_Test´Â ¹Ýµå½Ã True(0ÀÌ ¾Æ´Ñ °ª) °Å³ª False
(0) µÑ Áß Çϳª¸¦ ÆÇ´ÜÇÒ ¼ö ÀÖ´Â °ÍÀ̾î¾ß ÇÑ´Ù
ValueIfTure´Â ¹®ÀÚ¿, ³¯Â¥ ¶Ç´Â ¼ýÀÚ ÀÏ ¼ö ÀÖ´Ù
ValueIfFalse´Â ¹®ÀÚ¿, ³¯Â¥ ¶Ç´Â ¼ýÀÚ ÀÏ ¼ö ÀÖ´Ù
¸ÞÆ®¸¯½ºÇü ¸®Æ÷Æ®ÀÇ Ä®·³À» ¸¸µé °æ¿ì¿¡ À¯¿ëÇÏ´Ù
mysql> SELECT Name, GNP/Population * 1000 AS PerCapita,
-> LifeExpectancy,
-> IF(LifeExpectancy < 70, '<< -> FROM Country
-> WHERE Continent = 'Europe' AND GNP > 0
-> ORDER BY PerCapita LIMIT 10;
+------------------------+-----------+----------------+--------+
| Name | PerCapita | LifeExpectancy | Att |
+------------------------+-----------+----------------+--------+
| Moldova | 0.360502 | 64.5 | <<| Bosnia and Herzegovina | 0.715257 | 71.5 | |
| Ukraine | 0.835738 | 66.0 | <<| Macedonia | 0.836957 | 73.8 | |
| Albania | 0.942314 | 71.6 | |
| Belarus | 1.339781 | 68.0 | <<| Bulgaria | 1.486772 | 70.9 | |
| Yugoslavia | 1.597744 | 72.4 | |
| Romania | 1.699272 | 69.9 | <<| Russian Federation | 1.882532 | 67.2 | <<+------------------------+-----------+----------------+--------+
10 rows in set (0.03 sec)
CASE WHEN
mysql> SELECT CASE Code
-> WHEN 'USA' THEN '1. USA'
-> WHEN 'DEU' THEN '2. Germany'
-> WHEN 'FRA' THEN '3. Fraance'
-> WHEN 'GBR' THEN '4. UK'
-> ELSE '5. Rest of World' END AS Area,
-> SUM(GNP), SUM(population), SUM(SurfaceArea)
-> FROM Country
-> GROUP BY Area;
+------------------+-------------+-----------------+------------------+
| Area | SUM(GNP) | SUM(population) | SUM(SurfaceArea) |
+------------------+-------------+-----------------+------------------+
| 1. USA | 8510700.00 | 278357000 | 9363520.00 |
| 2. Germany | 2133367.00 | 82164700 | 357022.00 |
| 3. Fraance | 1424285.00 | 59225700 | 551500.00 |
| 4. UK | 1378330.00 | 59623400 | 242900.00 |
| 5. Rest of World | 15914029.65 | 5638602400 | 138441364.90 |
+------------------+-------------+-----------------+------------------+
5 rows in set (0.00 sec)
mysql> SELECT CASE
-> WHEN Code='USA' THEN '1. USA'
-> WHEN Continent='Europe' THEN '2. Europe'
-> WHEN Continent='Asia' THEN '3. Asia'
-> WHEN Continent='North America' THEN '4. N. America'
-> ELSE '5. Rest of World' END AS Area,
-> SUM(GNP), SUM(Population), SUM(SurfaceArea)
-> FROM Country
-> GROUP BY Area;
+------------------+------------+-----------------+------------------+
| Area | SUM(GNP) | SUM(Population) | SUM(SurfaceArea) |
+------------------+------------+-----------------+------------------+
| 1. USA | 8510700.00 | 278357000 | 9363520.00 |
| 2. Europe | 9498865.00 | 730074600 | 23049133.90 |
| 3. Asia | 7655392.00 | 3705025700 | 31881005.00 |
| 4. N. America | 1177927.20 | 204636000 | 14850950.00 |
| 5. Rest of World | 2517827.45 | 1199879900 | 69811698.00 |
+------------------+------------+-----------------+------------------+
5 rows in set (0.00 sec)
¼ö½Ä ÇÔ¼ö
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html
»ê¼ö °ü·Ã
- +, -, *, /
- PI()¿Í °°Àº »ó¼ö°ª
- ABS(), SIGN()
»ï°¢ÇÔ¼ö °ü·Ã
- SIN(), COS(), TAN()
- ASIN(), ATAN()
- DEGREES(), RADIANS()
·Î±×¿Í Á¦°ö°ü·Ã
- EXP, LOG, SQRT
¹Ý¿Ã¸² °ü·Ã
- CEILING()Àº ¿Ã¸²
- FLOOR()´Â ³»¸²
- ROUND()´Â °¡Àå °¡±î¿î
Á¤¼ö (0.5ÀÎ °æ¿ì OSº°·Î ´Ù¸§)
- TRUNCATE()´Â ¼Ò¼öÁ¡ ÀÌÇÏ ¹ö¸²
¹®ÀÚ¿ ÇÔ¼ö
CONCATÀº ¹®ÀÚ¿À» ÇÕÄ¡´Âµ¥ »ç¿ë. 1~n°³ÀÇ ÆĶó¸ÞÅ͸¦ °¡Áü
SUBSTRINGÀº À§Ä¡¸¦ ±âÁØÀ¸·Î ¹®ÀÚ¿ÀÇ ÀϺθ¦ »Ì¾Æ³½´Ù
SUBSTRING_INDEX´Â ±¸ºÐÀÚ¸¦ ±âÁØÀ¸·Î ¹®ÀÚ¿ÀÇ ÀϺθ¦
»Ì¾Æ³½´Ù
INSRT ´Ù¸¥ ¹®ÀÚ¿¿¡¼ ¸ÅĪµÇ´Â ¹®ÀÚ¿À» ã¾Æ³½´Ù
REPLACE´Â ´Ù¸¥ ¹®ÀÚ¿¿¡¼ ÇØ´ç ¹®ÀÚ¿À» ã¾Æ º¯°æÇÑ´Ù
LENGTH´Â ¹®ÀÚ¿ÀÇ ±æÀ̸¦ °è»êÇÑ´Ù
LIKE´Â ¹®ÀÚ¿À» ¿ÍÀϵåÄ«µå¸¦ ÀÌ¿ëÇÏ¿© ºñ±³ÇÒ ¶§ »ç¿ëµÈ´Ù
TRIMÀº ¹®ÀÚ¿¿¡¼ °ø¹éÀ» Á¦°ÅÇÑ´Ù
¿¹)
SUBSTRING_INDEX(¡®bjkim@nhncorp.com¡¯,¡¯@¡¯,1) bjkim
SUBSTRING_INDEX(¡®www.naver.com¡¯, ¡®.¡¯, -2) naver.com
REVERSE(¡®www.naver.com¡¯) moc.revan,www
UPPER(¡®naver¡¯) NAVER
RPAD(¡®bejay¡¯, 5, ¡®.¡¯) bejay.....
CHAR(17, 121,83,81, 76) MySQL
LEFT(¡®bejay kim¡¯, 3) bej
RIGHT(¡®bejay kim¡¯, 3) kim
TRIM(¡®Q¡¯ FROM ¡®QQQMySQL ABQQQ¡¯) MySQL AB
Date Time ÇÔ¼ö
NOW¿Í ¸î °¡Áö µ¿ÀǾîµé
³¯Â¥ °è»êÀ» À§ÇÑ DATE_ADD¿Í DATE_SUB
³¯Â¥ »çÀÌÀÇ °£°ÝÀ» °è»êÇϱâ À§ÇÑ DATEDIFF
³¯Â¥¸¦ ¿©·¯ °¡Áö Çü½ÄÀ¸·Î º¸¿©ÁÖ±â À§ÇÑ DATE_FORMAT
EXTRACT¸¦ »ç¿ëÇÏ¿© ³¯Â¥ Áß Æ¯Á¤ ºÎºÐ¸¸ ÃßÃâ
- DAY
- DAYOFMONTH
- DAYNAME
- WEEK
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2007-01-15 14:13:42 |
+---------------------+
1 row in set (0.03 sec)
mysql> SELECT NOW() + INTERVAL 2 DAY;
+------------------------+
| NOW() + INTERVAL 2 DAY |
+------------------------+
| 2007-01-17 14:13:57 |
+------------------------+
1 row in set (0.00 sec)
INTERVAL ¼ýÀÚ Å°¿öµå Çü½Ä
Å°¿öµå¿¡´Â YEAR, MONTH, DAY, HOUR, MINUTE, SECOND
DATE_FORMATÀ¸·Î ³¯ÀÚ Æ÷¸ä ¹Ù²Ù±â
mysql> SELECT webdate
-> DATE_FORMAT(webdate, ¡®%M %e, %Y¡¯) AS American,
-> DATE_FORMAT(webdate, ¡®%e, %c %Y¡¯) AS Swedish
-> FROM weblog
-> ORDER BY RAND() LIMIT 7;
+------------+-----------------------+------------+
| webdate | American | Swedish |
+------------+-----------------------+------------+
| 2001-10-11 | October 11, 2001 | 11.10 2001 |
| 2001-10-13 | October 13, 2001 | 13.10 2001 |
| 2001-10-03 | October 3, 2001 | 3.10 2001 |
| 2001-09-11 | September 11, 2001 | 11.9 2001 |
| 2001-08-31 | August 31, 2001 | 31.8 2001 |
| 2001-10-20 | October 20, 2001 | 20.10 2001 |
| 2001-08-29 | August 11, 2001 | 29.8 2001 |
+------------+-----------------------+------------+
7 rows in set (0.11 sec)
MAX-CONCAT Æ®¸¯
°¢ ´ë·úº°·Î ¾î¶² ³ª¶ó°¡ °¡Àå Àα¸°¡ ¸¹Àº°¡?
mysql> SELECT Continent,
-> SUBSTRING(MAX(CONCAT(LPAD(Population, 10, '0'),Name)), 11) AS Country,
-> 0 + LEFT(MAX(CONCAT(LPAD(Population, 10, '0'),Name)), 10) AS Population
-> FROM Country
-> GROUP BY Continent
-> ORDER BY Population DESC;
+---------------+----------------------------------------------+------------+
| Continent | Country | Population |
+---------------+----------------------------------------------+------------+
| Asia | China | 1277558000 |
| North America | United States | 278357000 |
| South America | Brazil | 170115000 |
| Europe | Russian Federation | 146934000 |
| Africa | Nigeria | 117081300 |
| Oceania | Australia | 18886000 |
| Antarctica | South Georgia and the South Sandwich Islands | 0 |
+---------------+----------------------------------------------+------------+
7 rows in set (0.02 sec)
IFNULL : NULLÀ» ´Þ¸® º¸¿©ÁÖ±â
mysql> SELECT Name,
-> IFNULL(LifeExpectancy, 'N/A') AS LifeE
-> FROM Country
-> WHERE Continent = 'Europe'
-> AND Region LIKE 'Nor%';
+------------------------+-------+
| Name | LifeE |
+------------------------+-------+
| Faroe Islands | 78.4 |
| Svalbard and Jan Mayen | N/A |
| Iceland | 79.4 |
| Norway | 78.7 |
| Sweden | 79.6 |
| Finland | 77.4 |
| Denmark | 76.5 |
+------------------------+-------+
7 rows in set (0.00 sec)
JOIN
mysql> SELECT Name, Capital
-> FROM Country
-> WHERE Region LIKE 'Nordic%';
+------------------------+---------+
| Name | Capital |
+------------------------+---------+
| Faroe Islands | 901 |
| Svalbard and Jan Mayen | 938 |
| Iceland | 1449 |
| Norway | 2807 |
| Sweden | 3048 |
| Finland | 3236 |
| Denmark | 3315 |
+------------------------+---------+
7 rows in set (0.00 sec)
mysql> SELECT Id, Name
-> FROM City
-> WHERE Id IN(901,938,1449,
-> 2807, 3048,3236,3315);
+------+------------------------+
| Id | Name |
+------+------------------------+
| 901 | Trshavn |
| 938 | Longyearbyen |
| 1449 | Reykjavk |
| 2807 | Oslo |
| 3048 | Stockholm |
| 3236 | Helsinki [Helsingfors] |
| 3315 | Kbenhavn |
+------+------------------------+
7 rows in set (0.14 sec)
Cartesian Product
NordicCountries¿Í NordicCapitalsÀ̶ó´Â Å×À̺íÀÌ ÀÖ´Ù°í
°¡Á¤ÇÏ°í µÎ Å×À̺íÀÇ ¸ðµç Row¸¦ ÇÕÄ£´Ù°í ÇÏ¸é ¾Æ·¡¿Í °°´Ù
mysql> SELECT *
-> FROM NordicCountries, NordicCapitals;
+------------------------+---------+------+------------------------+
| Name | Capital | Id | Name |
+------------------------+---------+------+------------------------+
| Faroe Islands | 901 | 901 | Trshavn |
| Svalbard and Jan Mayen | 938 | 901 | Trshavn |
| Iceland | 1449 | 901 | Trshavn |
| Norway | 2807 | 901 | Trshavn |
| Sweden | 3048 | 901 | Trshavn |
| Finland | 3236 | 901 | Trshavn |
| Denmark | 3315 | 901 | Trshavn |
| Faroe Islands | 901 | 938 | Longyearbyen |
| Svalbard and Jan Mayen | 938 | 938 | Longyearbyen |
| ¡¦ | ¡¦ | ¡¦ | ¡¦ |
| Denmark | 3315 | 3315 | Kbenhavn |
+------------------------+---------+------+------------------------+
INNER JOIN
mysql> SELECT Country.Name AS Contry, Country.Capital, City.Id, City.Name AS City
-> FROM Country INNER JOIN City
-> ON Capital = ID
-> WHERE Region LIKE 'Nordic%';
+------------------------+---------+------+------------------------+
| Contry | Capital | Id | City |
+------------------------+---------+------+------------------------+
| Faroe Islands | 901 | 901 | Trshavn |
| Svalbard and Jan Mayen | 938 | 938 | Longyearbyen |
| Iceland | 1449 | 1449 | Reykjavk |
| Norway | 2807 | 2807 | Oslo |
| Sweden | 3048 | 3048 | Stockholm |
| Finland | 3236 | 3236 | Helsinki [Helsingfors] |
| Denmark | 3315 | 3315 | Kbenhavn |
+------------------------+---------+------+------------------------+
49 rows in set (0.00 sec)
LEFT JOIN
SELECT ¡¦ FROM A LEFT JOIN B join_condition WHERE ¡¦
LEFT JOINÀ» ÇÒ °æ¿ì WHEREÀý¿¡ ¸Â´Â TABLE AÀÇ ¸ðµç
ROW´Â JOINÀ» À§ÇØ SELECTµÈ´Ù
- TABLE B¿¡ ¸ÅĪµÇ´Â °ªÀÌ ¾ø´Â row ¿ª½Ã ¸ðµÎ Æ÷ÇԵȴÙ
TABLE B¿¡ ¸ÅÄ¡µÇ´Â row°¡ ¾ø´Â °æ¿ì NULL·Î Ç¥½ÃµÈ´Ù
¼¹ö¿¡¼´Â TABLE A°¡ TABLE Bº¸´Ù ¾ðÁ¦³ª ¸ÕÀú ÀÐÈ÷°Ô µÈ´Ù
(¿À¶óŬ¿¡¼µµ JOINÇÒ¶§´Â Å« Å×ÀÌºí ¼ø¼´ë·Î ¾²¶ó°í µé¾úÀ½..Æ©´×)
JOIN Á¶°ÇÀº ¾Æ·¡¿Í °°Àº °ÍµéÀÌ ÀÖ´Ù
- ON A.col1 = B.col2
- USING (col1, col2)
mysql> SELECT Country.Name, City.Name
-> FROM Country
-> LEFT JOIN City
-> ON Capital=ID
-> WHERE Continent = 'Oceania'
-> ORDER BY Country.Name;
+--------------------------------------+--------------------+
| Name | Name |
+--------------------------------------+--------------------+
| American Samoa | Fagatogo |
| Australia | Canberra |
| Christmas Island | Flying Fish Cove |
| Cocos (Keeling) Islands | West Island |
| Cook Islands | Avarua |
| Fiji Islands | Suva |
| ¡¦ | ¡¦ |
| United States Minor Outlying Islands | NULL |
| Vanuatu | Port-Vila |
| Wallis and Futuna | Mata-Utu |
+--------------------------------------+--------------------+
28 rows in set (0.17 sec)
Âü°í : ´ë¿ë·® DB ¼Ö·ç¼Ç P.536~ http://blog.naver.com/kisow/20032827735
|