mysql -u username -p
在提示下输入对应用户的密码,即可进入MySQL命令行界面。
mysql -u username -p -D database_name
这里会直接连接到名为database_name
的数据库。
CREATE DATABASE database_name;
SHOW DATABASES;
USE database_name;
DROP DATABASE IF EXISTS database_name;
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
例如:
CREATE TABLE Users (
id INT PRIMARY KEY,
username VARCHAR(50),
password VARCHAR(100)
);
SHOW TABLES;
DESCRIBE table_name;
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
SELECT * FROM table_name;
UPDATE table_name SET column1 = new_value WHERE condition;
DELETE FROM table_name WHERE condition;
DROP TABLE IF EXISTS table_name;
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'username'@'localhost';
SET PASSWORD FOR 'username'@'localhost' = PASSWORD('new_password');
SHOW GRANTS FOR 'username'@'localhost';
mysqldump -u username -p database_name > backup.sql
mysql -u username -p database_name < backup.sql
以上涵盖了MySQL中常见的基本操作,包括连接服务器、数据库和表的管理、数据操作以及用户权限管理等。
SELECT column1, column2, ...
FROM table_name;
这将从table_name
表中选择指定的列进行查询。
SELECT * FROM table_name
WHERE condition;
例如,查找年龄大于30的所有用户:
SELECT * FROM Users WHERE age > 30;
SELECT * FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC];
示例:按年龄降序排列所有用户信息:
SELECT * FROM Users ORDER BY age DESC;
SELECT column1, COUNT(column2), AVG(column3)
FROM table_name
GROUP BY column1;
例如,计算每个年龄段用户的平均工资:
SELECT age_group, AVG(salary)
FROM (
SELECT id, FLOOR(age / 10) * 10 AS age_group, salary
FROM Employees
) AS grouped_data
GROUP BY age_group;
SELECT t1.column1, t2.column2
FROM table1 AS t1
JOIN table2 AS t2
ON t1.common_column = t2.common_column;
例如,查询每个员工及其所在的部门名称:
SELECT Employees.name, Departments.department_name
FROM Employees
JOIN Departments ON Employees.department_id = Departments.id;
SELECT * FROM table_name
WHERE column IN (SELECT column FROM another_table WHERE condition);
例如,找出和某个部门有相同员工数量的其他部门:
SELECT d1.department_name, COUNT(e1.id) as employee_count
FROM Departments d1
JOIN Employees e1 ON d1.id = e1.department_id
GROUP BY d1.id
HAVING COUNT(e1.id) = (
SELECT COUNT(*)
FROM Employees
WHERE department_id = (SELECT id FROM Departments WHERE department_name = 'Sales')
);
CASE
表达式或IF()
函数实现条件判断的功能SELECT
column1,
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE default_result
END AS computed_column
FROM table_name;
例如,计算员工工资等级:
SELECT
employee_name,
salary,
CASE
WHEN salary > 5000 THEN 'High'
WHEN salary BETWEEN 3000 AND 5000 THEN 'Medium'
ELSE 'Low'
END AS salary_level
FROM Employees;
MySQL中的IF()
函数接收三个参数:条件、满足条件时的结果和不满足条件时的结果。
SELECT
column1,
IF(condition, result_if_true, result_if_false) AS computed_column
FROM table_name;
例如,判断员工是否满勤:
SELECT
employee_name,
attendance_days,
IF(attendance_days = 30, 'Full Attendance', 'Not Full Attendance') AS attendance_status
FROM EmployeeAttendance;
以上两种方式都可以帮助我们在SQL查询中实现类似if-else的逻辑控制。在更复杂的场景下,可能需要结合多个CASE表达式或者嵌套使用IF()函数来完成更为复杂的业务逻辑处理。
以上是MySQL中的常见查询操作,涵盖了基本查询、条件筛选、排序、分组统计、连接查询以及子查询等场景