MYSQL-- query data from a table

Specifies the query column and row
look-up table all fields

  1. SELECT * FROM MAIL;

According to field filtration

  1. SELECT srcuser,srchost,t,size FROM mail;

By keyword query matching mode

  1. SELECT t,srcuser,srchost FROM mail WHERE srchost = 'venus';
  2. SELECT t,srcuser,srchost FROM mail WHERE srchost LIKE 's%';

where the query multiple conditions, different conditions can examine different columns.

  1. SELECT * FROM mail WHERE srcuser = 'barb' AND dstuser = 'tricia';

By CONCAT) combines (srcuser column and srchost

  1. 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
  2. SELECT DATE_FORMAT(t,'%M,%e,%Y'), CONCAT(srcuser,'@',srchost),size FROM mail;

Specify the name of the output column using the AS alias

  1. SELECT DATE_FORMAT(t,'%M,%e,%Y') as date_sent,CONCAT(srcuser,'@',srchost) AS sender,size FROM mail;
  2. SELECT DATE_FORMAT(t,'%M,%e,%Y') AS 'Date of message',CONCAT(srcuser,'@',srchost) AS 'Message sender',size AS 'Number of bytes' FROM mail;
  3. SELECT t,srcuser,dstuser,size/1024 AS kilobytes FROM mail WHERE size/1024 > 500;

Sort query results
one or more columns to sort

  1. SELECT * FROM mail WHERE dstuser = 'tricia' ORDER BY srchost,srcuser;
  2. SELECT * FROM mail WHERE size > 50000 ORDER BY size DESC;

Filter duplicate rows
generates a unique set of query results.

  1. SELECT DISTINCT srcuser FROM mail;
  2. SELECT COUNT(DISTINCT srcuser) FROM mail;
  3. SELECT DISTINCT YEAR(t),MONTH(t),DAYOFMONTH(t) FROM mail;

Handling NULL values
query NULL value and non-null value

  1. select * FROM expt WHERE score IS NULL;
  2. SELECT * FROM expt WHERE score IS NOT NULL;

Use IF () mapped to the NULL string Unknow

  1. SELECT subject,test,IF(score IS NULL,'Unknown',score) AS 'score' FROM expt;

View simplify the query
calculation expression to define a view

  1. SELECT DATE_FORMAT(t,'%M,%e,%Y') AS date_sent,CONCAT(srcuser,'@',srchost) AS sender,concat(dstuser,'@',dsthost) AS recipient,size FROM mail;
  2. 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;
  3. 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

  1. SELECT id,name,service,contact_name FROM profile INNER JOIN profile_contact ON id=profile_id;
  2. SELECT * FROM profile_contact WHERE profile_id = (SELECT id FROM profile WHERE name = 'Nancy');

LIMIT clause how many rows of data query

  1. SELECT * FROM profile LIMIT 1;
  2. SELECT * FROM profile LIMIT 3;
  3. SELECT * FROM profile ORDER BY birth LIMIT 1;
  4. SELECT * FROM profile ORDER BY birth DESC LIMIT 1;
  5. SELECT name,DATE_FORMAT(birth,'%m-%d') AS birthday FROM profile ORDER BY birthday LIMIT 1;
  6. SELECT * FROM profile ORDER BY birth LIMIT 2,1;
  7. SELECT * FROM profile ORDER BY birth DESC LIMIT 2,1;
  8. SELECT COUNT(*) FROM profile;

Returns the last 4 lines

  1. SELECT name,birth FROM profile ORDER BY birth DESC LIMIT 4;
  2. SELECT * FROM (SELECT name,birth FROM profile ORDER BY birth DESC LIMIT 4) AS T ORDER BY birth;

Guess you like

Origin blog.51cto.com/14766077/2484959