I have a question about inserting row order number by spesific order type.
Products table has OrderNumber field. I want to programaticly add new line to appropriate OrderNumber by its name. If the reference column would be integer it has to be easy like that
update products set OrderNumber=OrderNumber+1 where Price>555
Is there similar way for varchar field like
update products set OrderNumber=OrderNumber+1 where Name>'bla%'
Thank you
You can use STRCMP('text', 'text2')
update products
set OrderNumber=OrderNumber+1
where STRCMP(Name, 'bla') = 1;
I missunderstood your point. Can you try something like this?
SET #rownum:=0;
update
set OrderNumber=#rownum:=#rownum + 1
from products
order by Name;
You can simply run an update query with a sequential number like in here
Sounds like a bad design. Why not simply have a plain-jane auto_increment field and order by that? Every new record would by defnition have a higher ID than any of its predecessors.
you mean something like update products set OrderNumber=OrderNumber+1 where Name like 'bla%'
Related
I have a table like
user_id | settings
settings stored in json format like
{"user_lang":"en","shop_name":"PayBot"}
I need to add data like "user_id: 1" to each settings record.
In result it have to like this
{"user_lang":"en","shop_name":"PayBot","user_id: 1"}
How I understand I gave to open each record, encode to array, push to array "user_id: 1" and save.
But how to do this in php? :)
Can anybody to provide me a short example?
Thanks!:)
You can do it by different ways. One way is with JSON_SET.
UPDATE your_table_name
set settings = JSON_SET(settings, "$.user_id", 1)
WHERE user_id = 1;
You can remove the condition and add the column name user_id in place of static value 1 to apply all rows for settings column with their corresponding user_id value
REF.: https://database.guide/json_set-insert-or-update-values-in-a-json-document-in-mysql/
I got table promos, with field store_id_list (VARCHAR). what i want to achieve here is i want this promo can be available into multiple store in 1 record, instead using multiple record, the store_id_list value is the list of store_id separated by comma (ex: 1,4,5,7,)
Now, i want to get record of table promo, where i have single store_id value, ex: store_id=5 how can i do that in MySQL? can i do that in single query? if using LIKE then more likely i can get 15 or 45 instead of 5.
My advice is to normalize your tables, and have a db-record per store/promo. But it can be done like this:
Make sure you have commas at the beginning and end of the column value, like this:
store_id_list : ,1,4,5,7,
And then query like this:
... where store_id_list like '%,5,%'
I think you are looking for FIND_IN_SET function. For example:
SELECT * FROM tbl WHERE FIND_IN_SET('5', store_id) > 0;
Good day, I have a simple question here where I want to update my column whenever these two value are used.
UPDATE products SET description='YES' WHERE description='PENDING'
other than 'PENDING', I also want 'NO' to be included in this query for update.
What can I do? I want it to be updated on any row I update/click, Thank you.
update products
set description =
case
when description='PENDING' then 'YES'
else 'NO'
end
Are you looking for st. like this?
UPDATE products
SET description='YES'
WHERE description IN ('PENDING','NO') AND
id = 3
It sets description="YES" to all rows where description is equal to PENDING or NO.
try below:
UPDATE products SET description=if(<Your Condition to set Value YES>,'YES','NO') WHERE description='PENDING'
I have a table on MySQL, called Items with 2 columns: Id and Name. I want to show my items, filtering by its name first character. To do this, now I'm using this query:
Select Id,Name from Items WHERE Name LIKE 'a%'
I have 2 questions:
1) It is this the best method to achieve that?
2) To create the filter view, I want to know which characters have at least one item name starting with it. For example, I don't have any item name starting with "X". How could I know which ones have with a single query?
Thanks
If you have an Index on Name then your query is fine.
To get all characters having Names
Select substr(Name, 1, 1) as starting_character
from Items
group by starting_character
order by starting_character
You could run a large OR query like....
select * from Items where nobre like "A%" or nombre like "B%" or nombre like "C%" //etc etc
But if speed and table size is an issue, I would create another column, and just enter the letter value as an INT into that column. Kinda like an index.
So when you insert any new values, just code up a function to return the letter value of the first letter, and insert that into the new column.
Now you can query the column for any letter, based on that integer.
Hope that came out clearly, Im not very good at explaining things sometimes.....
This will give you all the first characters - only once (hence the DISTINCT)
SELECT DISTINCT SUBSTR(Name,1,1) FROM Items ORDER BY SUBSTR(Name,1,1)
Then you can use this to create your filters.
Hi so I was wondering what the best way to add or subtract from a field in my table would be. The way I know it would work is if I query the value do the addition and then UPDATE the value.
I would rather include the addition as part of the update query like:
UPDATE users SET points = +10
Or something like that. Is it possible?
You just need to name the column on the right hand side of the = operator:
UPDATE `users` SET `points` = `points`+10
Since there is no WHERE clause this will give all users 10 more points then they currently have.
You can just do this:
UPDATE users SET points = points + 10