mysql time method summary

1.MAKEDATE(year, dayofyear) returns the date given the year and day

select MAKEDATE(2017,23)

2. WEEKOFYEAR(DATE) returns the week number (1-53) for the given date

select WEEKOFYEAR('2017-05-13')

3.LEFT and RIGHT return the first or last len ​​characters of the string

select LEFT('2017-05-13',4)

4. WEEKDAY(DATE) returns the date index value of the week (0: Monday, 1: Tuesday...)

select WEEKDAY('2017-05-13')

5.replace into table1 (col1,col2)select.....Insert the value of the query into table1 according to the ID. If it already exists, delete it first, and then execute the insert operation.

Bulk edits possible

eg: table A field material number, date
table B field reference value, period, type (table, group, all)
requires updating table A, three cases
1. When the first three digits of the material number are equal to the reference value in table B, When the type field value of table B is group, the date of table A is updated according to the corresponding cycle of table B.
2. When the material number is exactly the same as the reference value in table B, and the type field value of table B is table, The date of table A is updated according to the corresponding cycle of table B.
3. When the reference value of the material number in table B cannot be matched, but there is only one record type field value in table B, the date of table A Update according to the cycle of the corresponding table B field type of all records,
for example, the date of table A is June 12, that is, select datepart(week, getdate())=24, the material number is 021-00001, and the table B is associated with The table is reference value = 021 cycle = 4 type = group, it means that 4 weeks is a cycle, and the cycle of 24 weeks is 21-24 weeks, then the date of table A becomes the first day of the 21st week, that is, May 21 , update table A

REPLACE INTO material(mNo, mDate)
SELECT mNo, MAKEDATE(
LEFT(mDate, 4), (WEEKOFYEAR(b.mDate) - IF((WEEKOFYEAR(b.mDate))%b.repeats, (WEEKOFYEAR(b.mDate))%b.repeats, b.repeats) +1)*7 + 1- WEEKDAY(MAKEDATE(
LEFT(mDate, 4), 1))) AS newDate
FROM 
(
SELECT material.*,ch.*
FROM material, ch
WHERE (
LEFT(material.mNo, 3) = ch.refer AND ch.types = 2) OR (material.mNo = ch.refer AND ch.types = 1) OR
 ((
SELECT COUNT(1)
FROM ch
WHERE
LEFT(mNo,3) =
LEFT(ch.refer,3))=0 AND (
SELECT COUNT(1)
FROM ch
WHERE types=3)=1 AND ch.types = 3))b


Guess you like

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