MAX(count) + 1 when inserting, MySQL - mysql

I currently use the below query to increment the third column count on every insert.
$DB2->query("INSERT INTO relations (item_ID,tag_ID,count)
SELECT '$ID', '$tag_id', MAX(count) + 1
FROM relations
WHERE tag_ID = '$tag_id';");
The problem is when there is no rows in the table and i try to insert, the Max(count) + 1 is just null. I've tried defining the default value as zero but still null. The column should be 1 on first insert.
How do i change the query, so if first insert then count is 1. I don't want to do a select query before because this code is in a loop.

add an ifnull(...,1)
"INSERT INTO relations (item_ID,tag_ID,count)
SELECT '$ID', '$tag_id', ifnull(MAX(count) + 1,1)
FROM relations
WHERE tag_ID = ''$tag_id';");

Related

How to handle versioning in sql with single query

Each Lead put data into pre_order_quotation table with a new version each time.
I am trying to get row count of all quotation by a lead & add 1 to it to get the version of new entry of quotation .
Was trying to achieve it in single query
insert into pre_order_quotation (lead_id,version,created_at,updated_at,file_name) values (1405,(select count(*) from pre_order_quotation where lead_id = 1405)+1,'2020-08-22 12:13:51','2020-08-22 12:13:51','dummy-5f410bffbcc80.pdf')
But i am getting the following error :
You can't specify target table 'pre_order_quotation' for update in FROM clause
How can i achieve it in single query?
Bury the increment a bit deeper..
insert into pre_order_quotation (lead_id,version,created_at,updated_at,file_name)
values (1405,
(select cnt from (select count(*) + 1 cnt from pre_order_quotation where lead_id = 1405) s ),
'2020-08-22 12:13:51','2020-08-22 12:13:51','dummy-5f410bffbcc80.pdf');

Select last two values from two IDs

I would like to select two specific values, the first value is the last inserted row where the ID_SENSOR is 1, and the second value is the last inserted row where the ID_SENSOR is 2.
My Database table:
My Query:
SELECT DATA FROM (SELECT * FROM registovalores WHERE ID_SENSOR = '1' OR ID_SENSOR = '2' ORDER BY ID_SENSOR DESC LIMIT 2) as r ORDER BY TIMESTAMP
My Query is printing the last value just from the ID_SENSOR 1, which it means that I'm only getting the last inserted values, and not the last inserted value from both IDS.
I would like to print my values like this:
ID_SENSOR 1 = 90
ID SENSOR 2 = 800
What do I need to change on my Query?
Thank you.
One method uses a correlated subquery:
SELECT rv.*
FROM registovalores rv
WHERE rv.ID_SENSOR IN (1, 2) AND
rv.TIMESTAMP = (SELECT MAX(rv2.TIMESTAMP)
FROM registovalores rv2
WHERE rv.ID_SENSOR = rv2.ID_SENSOR
);
You have to have two separate queries, one per sensor.
select id_sensor, data
from the_table
where id_sensor = 'sensor_1'
order by timestamp desc -- the latest value is the first to come
limit 1; -- only pick the top (latest) row.
If you want to query for more than one value in a single database roundtrip, consider using union all between several such queries.
Please note that such a query may return one row or zero rows, since data for a particular sensor may not be available yet.

Why am i getting "Subquery returns more than 1 row"

Hi I am making a webrowser game and I am trying to get monsters into my data base when I get the error:
Subquery returns more then 1 row
here is my code
INSERT INTO monster_stats(monster_id,stat_id,value)
VALUES
( (SELECT id FROM monsters WHERE name = 'Necroborg!'),
(SELECT id FROM stats WHERE short_name = 'atk'),
2);
any ideas how to fix this problem?
Try use LIMIT 1
INSERT INTO monster_stats(monster_id,stat_id,value) VALUES ((SELECT id FROM monsters WHERE name = 'Necroborg!' LIMIT 1),(SELECT id FROM stats WHERE short_name = 'atk' LIMIT 1),2);
Or you could use Insert from select, with join, if you have relations with 2 tables.
INSERT INTO monster_stats(monster_id,stat_id,value)
(SELECT monsters.id, stats.id, 2 as value FROM monsters
LEFT JOIN stats on monsters.id = stats.monsters_id
WHERE monsters.name = 'Necroborg!'
AND stats.short_name = 'atk'
)
MYSQL insert from select:
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
The problem is one or both of the following:
There is more than one monster named 'Necroborg!'.
There is more than on stat named 'atk'.
You need to decide what you want to do. One option (mentioned elsewhere) is to use limit 1 to get only one value from each statement.
A second option is to better specify the where clause so you get only one row from each table.
Another is to insert all combinations. You would do this with insert . . . select and a cross join:
INSERT INTO monster_stats(monster_id, stat_id, value)
SELECT m.id, s.id, 2
FROM (SELECT id FROM monsters WHERE name = 'Necroborg!') m CROSS JOIN
(SELECT id FROM stats WHERE short_name = 'atk');
A third possibility is that there is a field connecting the two tables, such as monster_id. But, based on the names of the tables, I don't think that is true.

SQL Query to retrieve next available ID from table

I have a table which contains a column called ticket_id and it contains values as follows:
ticket_id
STK0000000001
STK0000000002
STK0000000001
STK0000000003
STK0000000002
STK0000000001
The ticket_id value will repeat in certain rows, so it is not unique.
I am using this query to get the next available id, but I am not able to get it working. It always returns STK0000000002.
Any help is appreciated!
SQL:
SELECT
CONCAT('STK', LPAD(seq, 10, '0')) AS nextID
FROM
(SELECT
#seq:=#seq+1 AS seq,
num
FROM
(SELECT
CAST(SUBSTR(ticket_id, 4) AS UNSIGNED) AS num
FROM
sma_support_tickets
UNION ALL
SELECT
MAX(CAST(SUBSTR(ticket_id, 4) AS UNSIGNED))+2 AS num
FROM
sma_support_tickets
ORDER BY
num) AS ids
CROSS JOIN
(SELECT #seq:=0) AS init
) AS pairs
WHERE
seq!=num
LIMIT 1
Maybe I'm missing something in your question, but it seems that this should do it:
SELECT CONCAT('STK',
LPAD(MAX(SUBSTRING(ticket_id, 4)) + 1,
10,
'0')
)
FROM sma_support_tickets;
Try: This table must have one serial number or unique number or ID for the table for each row. Find out that unique number(primary key) through code and add 1 or increment that number, but not to the ticket_id as you are doing it now. so that it can move forward to next row.

MySQL - Update and Select in one query

I'm updating a table like so...
Update table Set count = count + 1 Where id = xx Limit 1
How to get the value of count without querying the table again? Can it be done in one query?
Thank you!
No.
Update does not return a result set.
However you can get the count without having to query the table
UPDATE `table` SET count = #count:= count + 1 WHERE id = 'xx' LIMIT 1;
SELECT #count as LastUpdateCount;