MySQL database combat learning - personal notes and summary

  • When using data in the database My'SQL subtraction,
  • Not impossible, to pay attention to: For example, I currently two tables like this:
    Here Insert Picture Description
    one is theorder used to store orders a product is used to store goods, want to ask stocks, you need to subtract theorder table with the product table amount Usually the sum of the number of the corresponding product.
    I started wrote this:
 select (product.amount-select sum(theorder.amount) from theorder where productID=3) as inventory where
    -> product.ID=3 from product;

Looked quite normal, people that is wrong with this statement:
ERROR 1064 (42000): by You have have AN error in your SQL syntax; the Check at The Manual that Corresponds to your MySQL Server Version for at The right syntax to use near 'the SELECT SUM ( theorder.amount) from theorder where productID = 3 ) as inventory where 'at line 1
and then I changed step by step to write:
Here Insert Picture Description
anyway, would not be parted with whole together.
Later, online search, respectively, select it, and then select the subtraction result came out, it would be:

 select(
 (select product.amount as s2)
 -  (select sum(theorder.amount) as s1 from theorder where productID=3)
 )
  as inventory from product where I

Here Insert Picture Description
(However, I would like to use this goose multiplication routine, do not.)

  • Xiao Ming and science, in order not to repeat the build table:
create table if not exists ohh
(......);
  • You can also set the character set when construction of the table, so that you can show the normal Chinese characters instead ???:
 create table  if not exists theOrder(ID int primary key auto_increment,clientID int,number int,orderDate date,productID int,amount int)default charset=utf8

Here Insert Picture Description

  • SQL database to find all the latest information of the actual recruits
    ideas:
    first need to find the biggest entry date employee information:
 select max(hire_date) from employees;

It's just found a "maximum entry date", we need to find the "maximum entry date corresponds employees":

  select * from employees
 where hire_date =
  (select max(hire_date) from employees);

(Note that the brackets.)

  • SQL database to find the actual time of recruits ranked third of all employee information.
    Ideas: First entry time employees in descending order, and then generates a "1" start increment column 1, and then select "1", "2", "3" corresponding to the employee information.
    There is a function "ROW_NUMBER" support this function: each row in the result set to add a line number, but this is only supported by version 8.0 later:
    Here Insert Picture Description
    should use the clause to limit the maximum number of rows to be returned constraint "1" with the offset specify an offset (from "0") "2":
select * from employees
order by hire_date desc
limit 1 offset 2;
Published 47 original articles · won praise 1 · views 1261

Guess you like

Origin blog.csdn.net/weixin_41750142/article/details/103879635