mysql commonly used statement six: query operations in DQL

1 Create 唯一索引and普通索引

The table structure is as follows:

Insert picture description here

demand

To first_namecreate 唯一索引uniq_idx_firstname, to last_namecreate 普通索引idx_lastname.

SQL statement

create unique index uniq_idx_firstname on actor(first_name);
create index idx_lastname on actor(last_name);

2 转义符use

The table structure is as follows

Insert picture description here

demand

Connect all employees' last_namesums first_namewith '(single quotes).

SQL statement

select concat(last_name, '\'', first_name) as name from employees;
select concat(last_name, '''', first_name) as name from employees;
select concat(last_name, "'", first_name) as name from employees;

The above three statements can all be processed. The difference is that 'the processing method is different, and the commonly used one is to use backslash escape \.

operation result

Insert picture description here

3 subqueries `nested

The table structure is as follows

Insert picture description here
Insert picture description here
Insert picture description here

demand

Find belongs to Action分类all the movies of the corresponding title, description.

SQL statement

select title, description from film 
where film_id in (select film_id from film_category where category_id = 
 (select category_id from category where name = "Action"));

Here's a query involves three tables title, descriptiononly the film表relevant, film_idthen with category表and film_category表related to the need 两重嵌套relationships.

operation result

Insert picture description here

4 Insert a record,忽略重复项

The table structure is as followsInsert picture description here

demand

Insert a piece of data for the table actor, if the data already exists, please ignore

actor_id first_name last_name last_update
‘3’ ‘ED’ ‘CHASE’ ‘2006-02-15 12:34:33’

SQL statement

insert ignore into actor values ('3', 'ED', 'CHASE', '2006-02-15 12:34:33');

Use ignorekeywords to avoid the problem of primary key duplication

operation resultInsert picture description here

5 Insert the query result as a record into the table

The table structure is as follows

Table actor
Insert picture description here

Table actor_name
Insert picture description here

demand

The actor表all first_nameand last_nameimporting actor_name表.

SQL statement

insert into actor_name select first_name, last_name from actor;

operation result

Insert picture description here

Guess you like

Origin blog.csdn.net/Awt_FuDongLai/article/details/114624543