Article directory
Share a mysql interview question
In the group today, a group friend posted an interview question, which aroused heated discussions among everyone. As a result, the blogger responded quickly and shared the interview question with everyone overnight.
Here's the thing:
A group of friends submitted their resumes in a certain recruitment software and saw such an invitation
Opening the link is such a question:
Then the group of friends posted the question in the group and asked the group of friends how to write. As a result, the group of friends began to discuss heatedly, which was a bit unsightly.
Although it is generally believed that this interview question has no practical use, I personally think that since this question is raised, the interviewer may have its needs, and it may be just a small example, similar to the actual scene. But it is said that the operation and maintenance still seldom participate in the table modification, and try to let the development to write the script modification.
We won't go into details, let's see how to answer the following interview questions.
First, I installed a mariadb database on the virtual machine to simulate the environment:
# 为了方便,直接使用yum安装
yum install mariadb-server -y
Create a table and insert data to simulate the table in the interview question
CREATE TABLE user (
name VARCHAR(20) NOT NULL,
sex ENUM('男','女') default '男') CHARSET=utf8;
insert user (name)values('陈佳');
insert user (name,sex)values('王辉','女');
insert user (name)values('胡前');
insert user (name)values('吴坚');
insert user (name)values('褚航');
insert user (name,sex)values('张宇红','女');
insert user (name)values('隽大伟');
MariaDB [test]> select * from user;
+-----------+------+
| name | sex |
+-----------+------+
| 陈佳 | 男 |
| 王辉 | 女 |
| 胡前 | 男 |
| 吴坚 | 男 |
| 褚航 | 男 |
| 张宇红 | 女 |
| 隽大伟 | 男 |
+-----------+------+
7 rows in set (0.00 sec)
MariaDB [test]>
Although the blogger already has the learning experience of the database, but it has been a long time since the actual operation, resulting in the knowledge is rusty =_=!
However, through my training on gpt, I got the desired result.
The following is the process of training gpt:
Finally got the desired result:
I am modifying the following according to the topic, and realized the result of the interview question:
SELECT name, CASE
WHEN sex = '男' THEN '1'
WHEN sex = '女' THEN '2'
END AS sex
FROM (
SELECT name, sex,
CASE
WHEN name IN ('陈佳', '张宇红') THEN 2
WHEN name IN ('胡前') THEN 4
WHEN name IN ('隽大伟') THEN 3
ELSE 1
END AS multiplier
FROM user
) AS temp
CROSS JOIN (
SELECT 1 AS number UNION ALL
SELECT 2 AS number UNION ALL
SELECT 3 AS number UNION ALL
SELECT 4 AS number
) AS multipliers
WHERE multipliers.number <= temp.multiplier;
The final effect:
Although I can't be sure what the interviewer expects to get, gpt at least achieves the effect of interview questions.
gpt is still relatively powerful, oriented to gpt operation and maintenance haha.
Finally, I wish you all the best of luck in finding the job you want as soon as possible. If you have any questions, you can comment and leave a message.
Personal blog: https://blog.waluna.top/