Specifies the query column and row
look-up table all fields
- SELECT * FROM MAIL;
According to field filtration
- SELECT srcuser,srchost,t,size FROM mail;
By keyword query matching mode
- SELECT t,srcuser,srchost FROM mail WHERE srchost = 'venus';
- SELECT t,srcuser,srchost FROM mail WHERE srchost LIKE 's%';
where the query multiple conditions, different conditions can examine different columns.
- SELECT * FROM mail WHERE srcuser = 'barb' AND dstuser = 'tricia';
By CONCAT) combines (srcuser column and srchost
- SELECT t, CONCAT (srcuser, ' @', srchost), size FROM mail;
specified query result column aliases
by the expression t reformatting date column, then use another expression srchost generated and connected srcuser - SELECT DATE_FORMAT(t,'%M,%e,%Y'), CONCAT(srcuser,'@',srchost),size FROM mail;
Specify the name of the output column using the AS alias
- SELECT DATE_FORMAT(t,'%M,%e,%Y') as date_sent,CONCAT(srcuser,'@',srchost) AS sender,size FROM mail;
- SELECT DATE_FORMAT(t,'%M,%e,%Y') AS 'Date of message',CONCAT(srcuser,'@',srchost) AS 'Message sender',size AS 'Number of bytes' FROM mail;
- SELECT t,srcuser,dstuser,size/1024 AS kilobytes FROM mail WHERE size/1024 > 500;
Sort query results
one or more columns to sort
- SELECT * FROM mail WHERE dstuser = 'tricia' ORDER BY srchost,srcuser;
- SELECT * FROM mail WHERE size > 50000 ORDER BY size DESC;
Filter duplicate rows
generates a unique set of query results.
- SELECT DISTINCT srcuser FROM mail;
- SELECT COUNT(DISTINCT srcuser) FROM mail;
- SELECT DISTINCT YEAR(t),MONTH(t),DAYOFMONTH(t) FROM mail;
Handling NULL values
query NULL value and non-null value
- select * FROM expt WHERE score IS NULL;
- SELECT * FROM expt WHERE score IS NOT NULL;
Use IF () mapped to the NULL string Unknow
- SELECT subject,test,IF(score IS NULL,'Unknown',score) AS 'score' FROM expt;
View simplify the query
calculation expression to define a view
- SELECT DATE_FORMAT(t,'%M,%e,%Y') AS date_sent,CONCAT(srcuser,'@',srchost) AS sender,concat(dstuser,'@',dsthost) AS recipient,size FROM mail;
- CREATE VIEW mail_view AS SELECT DATE_FORMAT(t,'%M,%e,%Y') AS date_sent,CONCAT(srcuser,'@',srchost) AS sender,CONCAT(dstuser,'@',dsthost) AS recipient,size FROM mail;
- SELECT date_sent,sender,size FROM mail mail_view WHERE size > 1000000 ORDER BY size;
Multi-table queries
Comparative profile table id column in the table and profile_contact profile_id column, the value of the same row are connected
- SELECT id,name,service,contact_name FROM profile INNER JOIN profile_contact ON id=profile_id;
- SELECT * FROM profile_contact WHERE profile_id = (SELECT id FROM profile WHERE name = 'Nancy');
LIMIT clause how many rows of data query
- SELECT * FROM profile LIMIT 1;
- SELECT * FROM profile LIMIT 3;
- SELECT * FROM profile ORDER BY birth LIMIT 1;
- SELECT * FROM profile ORDER BY birth DESC LIMIT 1;
- SELECT name,DATE_FORMAT(birth,'%m-%d') AS birthday FROM profile ORDER BY birthday LIMIT 1;
- SELECT * FROM profile ORDER BY birth LIMIT 2,1;
- SELECT * FROM profile ORDER BY birth DESC LIMIT 2,1;
- SELECT COUNT(*) FROM profile;
Returns the last 4 lines
- SELECT name,birth FROM profile ORDER BY birth DESC LIMIT 4;
- SELECT * FROM (SELECT name,birth FROM profile ORDER BY birth DESC LIMIT 4) AS T ORDER BY birth;