update users_match
set usr2 = case
when usr1 = 1 then 5
when usr1 = 2 then 6
when usr1 = 3 then 7
else usr2
end;
foreach($arr as $item){
if ( array_key_exists("tag", $item) && $item["tag"] == "plan" ) {
$link = $item["url"];
break;
}
}
CREATE TABLE `table1` (
`id` int,
`chat` int,
`q` int,
PRIMARY KEY (`id`, `chat`)
);
INSERT INTO `table1` ( `id`, `chat`, `q` )
VALUES ( 1, 12, 1 )
ON DUPLICATE KEY UPDATE `q` = `q` + 1;
update test
set
a = replace(a, 'slovo', ''), -- заменяем часть строки
b = if(b = 'slovo1', 'slovo2', b) -- заменяем строку по условию
;
select
user_name,
coalesce(likes, 0) likes
from users
left join likes on likes.user_id = users.id;
SELECT
JSON_ARRAYAGG(
JSON_OBJECT(
'image_src', images.image_src,
'image_alt', images.image_alt
)
) image,
product.*
FROM product
LEFT JOIN images ON images.prod_id = product.id
GROUP BY
product.id,
product.name;
create table questions (
id int primary key auto_increment,
question varchar(255)
);
create table question_options (
id int primary key auto_increment,
question_id int,
answer varchar(255)
);
select
q.question,
json_arrayagg(answer) answers
from questions q
join question_options qo on qo.question_id = q.id
group by q.id, q.question
;
+=============+============================+
| question | answers |
+=============+============================+
| Question 1? | ["Answer 1?", "Answer 2?"] |
+-------------+----------------------------+
| Question 2? | ["Answer 3?", "Answer 4?"] |
+-------------+----------------------------+
SELECT clients.name, SUM(balance*rate) as rub_balance
FROM clients
JOIN deposits d ON d.client_id = clients.id
JOIN currencies cur on d.currency = cur.id
GROUP BY clients.id, clients.name
;
SELECT
clients.name,
SUM(balance *
CASE d.currency
WHEN 'USD' THEN 75.64
WHEN 'EUR' THEN 91.67
ELSE 1
END
) as rub_balance
FROM clients
JOIN deposits d ON d.client_id = clients.id
sql_c = "INSERT INTO " + name + " (`date`,`time`,`timestamp`,`name`,`price`,`chng_1`,`chng_2`,`chng_3`,`chng_4`,`chng_5`,`chng_10`,`chng_15`,`chng_30`,`chng_60`,`chng_day`) VALUES %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
CREATE TABLE users (
id int primary key auto_increment,
name varchar(64),
status enum('single', 'married', 'divorced')
);
INSERT INTO users (name, status) VALUES
('Peter', 'single'),
('Basil', 'single'),
('Peter', 'married'),
('Basil', 'married'),
('Basil', 'divorced');
SELECT * FROM (
SELECT
name,
status,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY id DESC) rn
FROM users) user_status
WHERE rn = 1;
select
:id as person,
IF (person_1 = :id, person_2, person_1) person_contact
from persons
where person_1 = :id or person_2 = :id
;
SELECT
device_name,
COUNT(*) cnt
FROM users
WHERE
YEAR(created_date) = 2020 AND
year_of_birth < DATE_SUB(CURDATE(), INTERVAL 25 YEAR)
GROUP BY device_name;
SELECT *
FROM comment
WHERE (id = 1 AND reply_id IS NULL) OR reply_id = 1;
UPDATE
`products`
SET `price` = ROUND((`price_z` / 100) * (
CASE
WHEN `price_z` BETWEEN 1 AND 100000 THEN 110
WHEN `price_z` BETWEEN 100001 AND 200000 THEN 109
WHEN `price_z` BETWEEN 200001 AND 300000 THEN 108
ELSE 107
END
))
WHERE `date_price` = CURDATE();
select
oi.id,
(oi.price * coalesce(wi.quantity, 0)) wi_sum,
(oi.price * coalesce(ai.quantity, 0)) ai_sum,
(oi.price * coalesce(least(wi.quantity, ai.quantity), 0)) both_sum
from order_items oi
left join waybills_items wi on wi.order_item_id = oi.id
left join acts_items ai on ai.order_item_id = oi.id
order by oi.id;
UPDATE tbl
SET link = CONCAT(
'<p>[playerjs file:\"',
MID(LEFT(link, LOCATE('?', link, LOCATE('src="', link))-1), LOCATE('src="', link)+5),
'\"]</p>'
);
в языке SQL версии СУБД MySQL предпочтительно заключение названий таблиц и их полей в косые кавычки, поскольку это предотвращает путаницу с зарезервированными именами. Например, SELECT * FROM WHERE вызовет ошибку, а SELECT * FROM `WHERE` — нет (при условии, что существует таблица WHERE).
mysqli_query($mysqli, "CREATE TABLE `$game_name` (
`game_id` INT(11) DEFAULT $output[id],
`name` VARCHAR(40) NOT NULL,
`image` VARCHAR(100),
`descr` VARCHAR(100) NOT NULL,
`percents` VARCHAR(6),
`making` TEXT(1000),
`vid_link` VARCHAR(100)
)");