当前位置:网站首页>Basic commands of MySQL

Basic commands of MySQL

2022-07-06 06:41:00 luckyyuanyuan

sql Case difference
 Insert picture description here
Create data table :

CREATE TABLE IF NOT EXISTS `runoob_tbl`(
   `runoob_id` INT UNSIGNED AUTO_INCREMENT,
   `runoob_title` VARCHAR(100) NOT NULL,
   `runoob_author` VARCHAR(40) NOT NULL,
   `submission_date` DATE,
   PRIMARY KEY ( `runoob_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

1.  Set the attribute of the field to  NOT NULL,  When operating the database, if the data entered in this field is NULL , You're going to report a mistake .
2. AUTO_INCREMENT Define properties that are listed as self incrementing , Generally used for primary key , The value will be added automatically 1.
3. PRIMARY KEY Keywords are used to define columns as primary keys .  You can use multiple columns to define the primary key , Columns are separated by commas .
4. ENGINE  Set up the storage engine ,CHARSET  Set encoding .

According to “PH” Field lookup data , And limit the display of only the first 1000 That's ok

select * from dm_oss.d_f_oss_device_service_info where device_id like '%PH%' limit 1000

Query the values in a column of data ( duplicate removal )

select distinct device_id from dm_oss.d_f_oss_device_service_info where device_id

Specify the inserted column name and value to insert :

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

stay “device_id ” The query attribute value in the field contains “PH” The data of , And limit only to show 1000 That's ok

select * from device_service_info where device_id like '%PH%' limit 1000 

 Insert picture description here

select * from device_basic_info where device_id like '%PH%' limit 1000

 Insert picture description here

Case a : Multi table joint query and statistics

surface 1:device_service_info Insert picture description here
surface 2:device_basic_info Insert picture description here
step 1: Inquire about 1.4.9 Version of class
// stay “software_version ” The query attribute value in the field contains “1.4.9” The data of

select device_id, description, software_version from device_basic_info having software_version like '%1.4.9%' 

 Insert picture description here
step 2: according to service_start_time Field to select data for a certain period of time

select device_id, service_id from device_service_info having service_start_time between "${day_start}" and "${day_end}"

 Insert picture description here
step 3: Calculate the service times of each device in a specified period of time

select device_id, count(service_id) as service_sum from (select device_id, service_id from device_service_info having service_start_time between "${day_start}" and "${day_end}") group by device_id

 Insert picture description here
step 4: Splice the information of two tables , Adopt internal connection , Take their intersection ;

select t1.device_id, t1.service_sum, t2.description, software_version from ((select device_id, count(service_id) as service_sum from (select device_id, service_id from dm_oss .d_f_oss_device_service_info having service_start_time between "${day_start}" and "${day_end}") group by device_id) t1) inner join ((select device_id, description, software_version from dm_oss.d_f_oss_device_basic_info having software_version like '%1.4.9%') t2 )on t1.device_id = t2.device_id 

 Insert picture description here

原网站

版权声明
本文为[luckyyuanyuan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060626320636.html