UPDATE `table` SET `updated` = 0 WHERE `user_id` = $uid
INSERT INTO `table` (`user_id`, `obj_id`, `updated`) VALUES ($uid, $oid, 1)
ON DUPLICATE KEY UPDATE `updated` = 1
DELETE FROM `table` WHERE `user_id` = $uid AND `updated` = 0
SELECT @left_key := `left_key`, @right_key := `right_key`
FROM `tree`
WHERE `id` = $id;
SELECT *
FROM `tree`
WHERE `left_key` <= @left_key AND `right_key` >= @right_key
ORDER BY `left_key`;
$byFoo = 0; $foo = '';
$byBar = 0; $bar = '';
if (isset($_GET['foo']) {
$foo = $_GET['foo'];
$byFoo = 1;
}
if (isset($_GET['bar']) {
$bar = $_GET['bar'];
$byBar = 1;
}
$query = "SELECT * FROM `baz` WHERE (? = 0 OR `foo` = ?) AND (? = 0 OR `bar` = ?)";
$mysqli->bind_param('isis', $byFoo, $foo, $byBar, $bar);
INSERT INTO `table` (`key_field`, `upd_field1`, `upd_field1`)
VALUES ('key1', 'f1_val1', 'f2_val1'), ('key2', 'f1_val2', 'f2_val2')
ON DUPLICATE KEY UPDATE
`upd_field1` = VALUES(`upd_field1`),
`upd_field2` = VALUES(`upd_field2`);
SELECT `ui`.`id` AS `image_id`,
`ui`.`user_id` AS `user_id`,
`ui`.`create_date` AS `image_create_date`,
`ui`.`name` AS `image_name`,
`ui`.`title` AS `image_title`,
`ui`.`descr` AS `image_descr`,
`ui`.`views` AS `image_views`,
`ui`.`plus` AS `image_plus`,
`ui`.`minus` AS `image_minus`,
`bu`.`first_name` AS `user_first_name`,
`bu`.`last_name` AS `user_last_name`,
`bu`.`photo` AS `user_photo`,
COUNT(`c`.`id`) AS `com_cnt`
FROM `user_images` AS `ui`
LEFT JOIN `bpl_users` AS `bu` ON `bu`.`id` = `ui`.`user_id`
LEFT JOIN `comments` AS `c` ON `c`.`image_id` = `ui`.`id`
GROUP BY `ui`.`id`
ORDER BY `ui`.`create_date` DESC LIMIT 9
CREATE INDEX `image_id_idx` ON `comments` (`image_id`);