Commands are entered into a multi-line buffer (retained across invocations of MySQL!).
Send command buffer to server by terminating with the delimiter character
( default ;) or \g aka go .
AUTOCOMMIT is enabled by default.
For transaction-safe tables SET AUTOCOMMIT=0;commit will permit ROLLBACK.
HOWEVER Warning (Code 1196): Some non-transactional changed tables couldn't be rolled back
source \ | . file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The last 50 previous lines of the buffer can be recalled into current buffer (even across invocations of MySQL).
[^] or [F5]scroll to previous buffer line (repeatedly)
[F7] Present prior buffer lines for insertion into current buffer
N.B. If line inserted contains delimiter, buffer is sent. sounds pretty dangerous to me.
[F9]copy prior buffer line nn into current buffer.[F2] copy buffer to char [F4] delete buffer to char
INSERT,REPLACE, UPDATE, DELETE
Merge (an Oracle extension is not available in MySQL
SELECT [ALL|DISTINCT|DISTINCTROW]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr, ...
[FROM table_references
[WHERE where_condition]
[GROUP BY [column | expr | position] [ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY [column | expr | position] [ASC | DESC], ...]
[LIMIT [[offset,] row_count | row_count OFFSET offset]]
[PROCEDURE procedure_name(argument_list)]
[INTO [OUTFILE| DUMPFILE] 'file_name' export_options
| @var_name [, @var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]]>]
SELECT can retrieve rows computed without reference to any table.
where_condition :=
| column
|
GROUP BY (Aggregate) Functions
ignores nulls
Using a group function in a statement without a GROUP BY clause, it is equivalent to grouping on all rows.
GROUP BY and HAVING with Hidden Columns
GROUP BY COUNT(DISTINCT) count of a number of different values COUNT() number of rows returned GROUP_CONCAT() concatenated string SUM() MIN() AVG() MAX() STD() population standard deviation STDDEV_POP() population standard deviation STDDEV_SAMP() sample standard deviation STDDEV() population standard deviation VAR_POP() population standard variance VAR_SAMP() sample variance VARIANCE() population standard variance BIT_AND() bitwise and ; BIT_OR() BIT_XOR() |
INSERT [INTO] table SET column=[expr | DEFAULT], …
OR
OR
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
[(column,…)]
[ VALUES | VALUE] ([expr | DEFAULT]…),(…),…
[ ON DUPLICATE KEY UPDATE column=expr [, col_name=expr] … ]
INSERT … [INTO] tbl_name [(column,…)]
SELECT …
VALUES(column,…) ON DUPLICATE KEY UPDATE
a=VALUES(a), b=VALUES(b);
REPLACE is DELETE then INSERT← MySQL extension
REPLACE [INTO] table [(column,…)]
VALUE[S] ({expr | DEFAULT},…),(…),…OR
REPLACE [INTO] table SET column={expr | DEFAULT}, …OR
REPLACE [INTO] table [(column,…)] SELECT …
REPLACE [INTO] table [(column,…)]
UPDATE [LOW_PRIORITY] [IGNORE†] table
SET column1 = [expr1|DEFAULT]
DELETE FROM table WHERE column = expr LIMIT 1
RESET |SET
DO
SHOW
UNION JOIN
alter
CACHE INDEX
EXPLAIN
PURGE FLUSH KILL
LOAD DATA LOAD INDEX
Functions:
Date and Time
Dates must be in year-month-day order.SELECT CURDATE(); -> '2008-06-13' SELECT CURDATE() + 0; -> 20080613 CURRENT_DATE and CURRENT_DATE() are synonyms for CURDATE(). 'HH:MM:SS' or HHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. SELECT CURTIME(); -> '23:50:26' SELECT CURTIME() + 0; -> 235026.000000Set a shell variable to last month export lastMonth=`echo "select CURDATE() - INTERVAL 1 month;" |mysql |tail -n1 `Two-digit years 00-69 are converted to 2000-2069. 2009-11-31 use SERVERMODE!!
ALLOW_INVALID_DATES my.cnf sql-mode="no ALLOW_INVALID_DATES" or TIME Formats accepted:
Examples: hour 01..12
NO space 24H 0min | hour 1..12
↓ | 01 | /
Select Time_Format('100:00:00', '%H:%i %h %I %l');
'100: 100 04 04 4' time is '-838:59:59' to '838:59:59' allowing for elapsed, timeBefore and timeafter as much as 34 days
(arranged by function, not alphabetically)
%m Month, numeric 00..12; %c 0..12; %M January..December ; %b Jan..Dec
(how to blank padded?? ie ' 1, 2, 3' vs '01,02,03' ?? )
%d 00..31; %e 0..31; %D Day suffixed (0th, 1st, 2nd, 3rd, …)
%y YY; %Y CCYY %C cc century 00..99
%h 01..12 %I Hour 01..12; %l 1..12 %p AM or PM
%H 00..23; %k 0..23
%i Minutes 00..59
%S Seconds 00..60; %s 00..60 The 60 is necessary to accommodate a leap second
%f Microseconds (000000..999999)
%r Time, 12-hour (hh:mm:ss .M ; %T 24-hour (hh:mm:ss)
%w Day of the week (0=Sunday..6=Saturday); %W (Sunday..Saturday) %a (Sun..Sat)
%j Day of year (001..366)
%U Week (00..53), where Sunday is the first day of the week %u Week (00..53), where Monday
%V Week (01..53), where Sunday is the first day of the week; used with %X %v Week (01..53), where Monday …
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%F %Y-%m-%d
%p locale's upper case AM or PM (blank in many locales)
ADDDATE(e1,e2) Add intervals to a date ADDTIME(e1,e2) update racers set end= addtime(start, "1:51:44" ) where name like "Joe %"; DATE_ADD(e1,e2) Add time values (intervals) to a date value DATEDIFF() Subtract two dates DATE_SUB(d, INTERVAL n units), SUBDATE(d,days) Subtract two dates FROM_DAYS() a day number to a date MAKEDATE(year,dayofyear) MAKETIME(hour,minute,second) SUBTIME('2007-12-31 23:59:59.999999','02:00:00.999998') PERIOD_ADD(yymm,months) Add a period to a year-month PERIOD_DIFF() number of months between periods STR_TO_DATE() string to a date GET_FORMAT() date format string TIME_FORMAT() Format as time ( no date info, use DATE_FORMAT DATE_FORMAT() SEC_TO_TIME() seconds to 'HH:MM:SS' format TIME_TO_SEC() to seconds TIMEDIFF() Subtract time TIMESTAMP (dt2 ) date or datetime expression; TIMESTAMP (dt1, dt2 ) sum of the arguments TIMESTAMPADD(unit,i, dt) Add an interval to a datetime expression TIMESTAMPDIFF(unit, dt1,dt2) Subtract an interval TO_DAYS() date argument converted to days EXTRACT Extract part of a date DATE() Extract the date part of a date or datetime expression DAY() DAYOFMONTH() 0-31 julian use: DAYOFYEAR() 1-366 HOUR() TIME() MINUTE() SECOND() 0-59 MICROSECOND() MONTH() MONTHNAME() DAYOFWEEK() WEEKDAY() weekday index ; DAYNAME() QUARTER() YEAR() WEEK() WEEKOFYEAR() 0-53 YEARWEEK() year and week LAST_DAY of the month UNIX_TIMESTAMP() FROM_UNIXTIME() Format UNIX timestamp as a date SYSDATE() time at which the function executes CURDATE() CURRENT_DATE() CURRENT_TIMESTAMP() Synonyms for NOW() CURTIME() CURRENT_TIME() LOCALTIME() LOCALTIMESTAMP() UTC_DATE() current UTC date; UTC_TIME() UTC_TIMESTAMP() current UTC date and time CONVERT_TZ() from one timezone to another days=DATEDIFF(expr1,expr2) DATE_ADD(date,INTERVAL expr unit) aka ADDDATE(SELECT DATE_ADD('2100-12-31 23:59:59',INTERVAL '1:1' MINUTE_SECOND); -> '2101-01-01 00:01:00'
Also permitted (no
MICROSECOND , SECOND , MINUTE, HOUR , DAY , WEEK, MONTH , QUARTER ,
ADDTIME(e1,e2)
subtract date and time functions |
Information
Numeric
String
Encryption
Miscellaneous
CHAR BYTE
TRUE FALSE
DUAL
Column Types | ||||||||
|
AUTO_INCREMENT
BINARY |
DATE TIME DATETIME TIMESTAMP YEAR |
ENUM DECIMAL NUMERIC FLOAT DOUBLE |
INT TinyINT SmallINT MediumINT BigINT BOOLEAN† |
CHAR VARCHAR 0‑32766
TEXT† |
BLOB TinyBLOB
MediumBLOB | |||
|
DROP [COLUMN] column
DROP {INDEX|KEY} index_name
DISABLE KEYS | ENABLE KEYS
table_options
| ||||||||||
CREATE Declare columns to be NOT NULL saves time
| ||||||||||||||||||||||||||||||||
DATABASE
| ||||||||||||||||||||||||||||||||
INDEX: CREATE| DROP
|
FunctionsFUNC(arg No space permitted after (
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CONCAT('abc','def',7, …) numeric argument is converted to string
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
CONCAT_WS(sep, …) concatenate with separator
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
ELT(N,str1,str2,str3,...) Elment in LisT ;Returns string at index number
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
MAKE_SET() set of comma-separated strings that have the corresponding bit in bits set
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
EXPORT_SET(bits,on,off[,separator[,number_of_bits]]) string with ons for bits set in the value bits, and off for unset bit
mysql> SELECT EXPORT_SET(5,'Y','N',',',4);
-> 'Y,N,Y,N'
mysql> SELECT EXPORT_SET(6,'1','0',',',10);
-> '0,1,1,0,0,0,0,0,0,0'
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Control Flow | |||
| CASE Case operator | |||
IF() If/else construct
IF(expr1,expr2,expr3) If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3. IF() returns a numeric or string value, depending on the context in which it is used. |
ABS() | absolute value | ||||||||||||||||||||||||||||||||||||||||||||||||
CEIL() | , | ||||||||||||||||||||||||||||||||||||||||||||||||
CEILING() | smallest integer value not less than the argument | ||||||||||||||||||||||||||||||||||||||||||||||||
FLOOR() | largest integer value not greater than the argument | ||||||||||||||||||||||||||||||||||||||||||||||||
ROUND() | Round | ||||||||||||||||||||||||||||||||||||||||||||||||
RAND() | random floating-point value | ||||||||||||||||||||||||||||||||||||||||||||||||
SIGN() | sign | ||||||||||||||||||||||||||||||||||||||||||||||||
|
LOAD_FILE(ifile) from server. requires file priviledge.
Geographic featuresGEOMETRY GEOMETRY HIERARCHY SPATIAL |
| RESET SLAVE
SHOW SLAVE STATUS
START/stop SLAVE
SET GLOBAL SQL_SLAVE_SKIP_COUNTER | ||||||||
| DESCRIBE table [column | wild] | ||||||||
Transactions Topics:HANDLER, ISOLATION, LOCK, SAVEPOINT, START TRANSACTION, | ||||||||
AdministrationANALYZE CHECK* CHECKSUM CONSTRAINT CREATE USER user IDENTIFIED BY PASSWORD 'password' GRANT SELECT UPDATE ON TABLE tbl_name RENAME USER REVOKE SET PASSWORD FLUSH QUERY CACHE MERGE REPAIR BACKUP TABLE RESTORE SHOW CREATE VIEW |
See My SQL admin for addtional details.
show variables
show variables like 'sql%'
$q ="insert …| SELECT …" $execute = $connect->query($q); $ar=$execute->affectedrows($q); $nr=$execute->numrows(); $nf=$execute->numfields();keep fetching rows as long as there are rows to fetch.
while(@results = $execute->fetchrow() ){ $r0=$results[0]; … }