MySQL Basic Operations

Eh, this is mainly the MySQL grammar of today's meeting. After learning Oracle, I forgot MySQL. Before, an interviewer sent SQL Server test questions and directly gg. embarrassment......

/*Create a table of check-in guests*/
CREATE TABLE guestStayInfo(
    gsiID INT PRIMARY KEY AUTO_INCREMENT, /*Number of check-in information, unique primary key constraint self-increasing*/
    guestSource VARCHAR(50) NOT NULL, /*client source*/
    CheckIn VARCHAR(20), /*Check-in time*/
    CheckOut VARCHAR(20), /*Checkout time*/
    linkman VARCHAR(20), /*Contact*/
    orderamount INT, /*Order amount*/
    needCash INT, /*Amount to be paid*/
    OperatorID INT, /*Operator, the foreign key corresponds to the corresponding employee*/
    CONSTRAINT FK_emp FOREIGN KEY(OperatorID) REFERENCES employee(empID)
)AUTO_INCREMENT=1;

/* try to insert data */
INSERT INTO guestStayInfo(guestSource, CheckIn, CheckOut, linkman, orderamount, needCash, OperatorID)
VALUES ('Ba Le Rabbit', '2018/01/02', '2018/04/02', 'Mr. Wang', 300, 45, 1);
INSERT INTO guestStayInfo(guestSource, CheckIn, CheckOut, linkman, orderamount, needCash, OperatorID)
VALUES ('58.com', '2018/02/03', '2018/04/04', 'Mr. Zhang San', 260, 20, 2);
/* Query all data */
SELECT * FROM guestStayInfo;
/*Add external link to view operator*/
SELECT * FROM guestStayInfo g INNER JOIN employee e WHERE g.OperatorId=e.empId;
SELECT gsiId AS 'guest record number', guestSource AS 'guest source', CheckIn AS 'check in time', CheckOut AS 'check out time', linkman AS 'contact person',
orderamount AS 'deposit required', needCash AS 'supplementary deposit', empName AS 'operator name' FROM guestStayInfo g INNER JOIN employee e WHERE g.OperatorId=e.empId;
/*View all the guests from 58.*/
SELECT * FROM guestStayInfo WHERE guestSource='58同城';

/* Output all guest information from low to high according to the replenishment deposit*/
SELECT * FROM guestStayInfo ORDER BY needCash DESC ;
/* Output all guest information from high to low according to the required deposit*/
SELECT * FROM guestStayInfo ORDER BY orderamount ASC;

/* According to the grouping, the sum of the deposit and the supplementary deposit will be output from low to high*/
SELECT gsiId AS 'guest record number', guestSource AS 'guest source', CheckIn AS 'check in time', CheckOut AS 'check out time', linkman AS 'contact person',
orderamount AS 'Deposit required', needCash AS 'Additional deposit',(orderamount+needCash) 'The sum of the two' FROM guestStayInfo ORDER BY (orderamount+needCash) DESC;

/*Query the guests who stayed before 2018/01/01*/
SELECT * FROM guestStayInfo WHERE CheckIn<'2018/01/01';
/*Query the guests who check out after 2018/04/05*/
SELECT * FROM guestStayInfo WHERE CheckOut>'2018/04/05';

/*Check the average amount of deposit required*/
SELECT AVG(orderamount) AS 'Average amount of deposit required' FROM guestStayInfo;
/* Check the total deposit required */
SELECT SUM(orderamount) AS 'Deposit Sum' FROM guestStayInfo;
/* Check the maximum repayment deposit */
SELECT MAX(orderamount) AS 'Maximum supplementary deposit' FROM guestStayInfo;
/* Check the minimum supplementary deposit */
SELECT MIN(orderamount) AS 'Minimum supplementary deposit' FROM guestStayInfo;

/* Count the number of lines in total */
SELECT COUNT(gsiId) FROM guestStayInfo;

Well, that's the easiest thing to do. Adds, deletes and modifies QAQ no more ╮(╯_╰)╭


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324774589&siteId=291194637