CREATE TRIGGER `after_update_table_name` AFTER UPDATE ON `table_name`
FOR EACH ROW BEGIN
IF `OLD`.`field_name` <> `NEW`.`field_name` THEN
# Your action
END IF;
END
CREATE TRIGGER `before_insert_table_name` BEFORE INSERT ON `table_name`
FOR EACH ROW
BEGIN
SET `NEW`.`z` = `NEW`.`x` + `NEW`.`y`;
END
CREATE TRIGGER `before_update_table_name` BEFORE UPDATE ON `table_name`
FOR EACH ROW
BEGIN
IF (`OLD`.`x` <=> `NEW`.`x`) = 0 OR (`OLD`.`y` <=> `NEW`.`y`) = 0 THEN
SET `NEW`.`z` = `NEW`.`x` + `NEW`.`y`;
END IF;
END
SELECT * FROM `table_name` WHERE `column_name` IN ('A', 'B', 'C') ORDER BY FIELD (`column_name`, 'A', 'B', 'C')
SELECT * FROM `table_name` WHERE `column_name` IN ('A', 'B', 'C') ORDER BY FIND_IN_SET (`column_name`, 'A,B,C')
// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
foreach (range(0, 12) as $number) {
echo $number;
}
implode(',', range(41, 44)); // 41,42,43,44
SELECT last_insert_rowid()
$users[] = 1; // ID пользователя
$q = "SELECT * FROM table regexp '[[:<:]](" . implode( '|', $users) . ")[[:>:]]";
users
id, first_name, last_name, ...
chats
id, user_id, name, ...
chats_users
chat_id, user_id
$q = "SELECT * FROM table WHERE FIND_IN_SET('1', `users`)"; // 1 - ID пользователя
SELECT SUM(`visits`) AS `visits` FROM `statistics` WHERE `date` BETWEEN UNIX_TIMESTAMP(CONVERT_TZ(FROM_UNIXTIME('1510693200'), @@global.time_zone, @@session.time_zone)) AND UNIX_TIMESTAMP(CONVERT_TZ(FROM_UNIXTIME('1510779599'), @@global.time_zone, @@session.time_zone))
SELECT SUM(`visits`) AS `visits` FROM `statistics` HAVING UNIX_TIMESTAMP(CONVERT_TZ(FROM_UNIXTIME(`date`), @@session.time_zone, @@global.time_zone)) BETWEEN '1510693200' AND '1510779599'
countries (id, name)
regions (id, country_id, name)
cities (id, country_id, region_id, name)