I have a MySQL database where I need add multiple values, but I need to specify a range of values just in one column.
I'm not an expert but the logic for what I need is this:
INSERT INTO ps_category_product (id_category, id_product, position) VALUES (56,BETWEEN 3 AND 5,0);
I know that is not possible to use BETWEEN in a insert into query, but how can I do something similar to specify the range of the id_product row?
How can I add all the values between 2 numbers like 3 and 5 (3,4,5) or 50 and 100 (50,51,52,...,98,99,100)
You can create a new table called range with two attributes representing the "range"... then you create a relationship(one to one) between "ps_category_product" and "new_table"...this way, in your ps_category_product table you will have a foreign key that references two values...
No, you can't use range of values while inserting. Rather, you will have to validate your insert values using a BEFORE INSERT TRIGGER something like below (a sample)
CREATE TRIGGER validaterangeTrigger
BEFORE INSERT
ON ps_category_product
FOR EACH ROW BEGIN
IF (NEW.id_product NOT BETWEEN 3 AND 5) THEN
SET NEW.id_product = 3; <-- setting a default value 3
END IF
Then you can call your INSERT statement
INSERT INTO ps_category_product (id_category, id_product, position) VALUES (56,4,0);
If the id_product value is not between 3 and 5 then trigger will set it to default value 3. You can as well choose to throw an error if validation fails.
Related
I have two columns in my mysql table, equipment and orderno, Here equipment number is manually inserted, and they are in the form C1234,C3212 etc.
I want to strip the C from equipment column and insert the remaining number to orderno column. I have seen that mysql substring_index() can effectively get substring but I am not sure how to make it automtically do the changes, when the equipment column changes.
You could use two triggers, one that's fired before an INSERT, and one that's fired before an UPDATE, to automatically update orderno based on equipment column:
CREATE TRIGGER upd_your_table BEFORE UPDATE ON your_table
FOR EACH ROW
SET new.orderno=substring(new.equipment, 2)
;
CREATE TRIGGER ins_your_table BEFORE INSERT ON your_table
FOR EACH ROW
SET new.orderno=substring(new.equipment, 2)
;
To update existing values, you could use this:
UPDATE your_table SET orderno=substring(equipment, 2)
See this fiddle
Try this ::
UPDATE myTable set orderno= REPLACE(equipment, 'C', '')
Is there a way to set the value of another column to primary key (auto increment)?
Basically what I am trying to achieve is this
ID Stuff
---- ------
1 1
2 324
3 64
4 94
5 ...
Now when I am adding the the fifth row with a query like
INSERT into TABLE values(NULL, NULL);
So when the second value is NULL I want it to be equal to ID.
I tried INSERT triggers but it doesnt work. Any ideas?
I don't think you can do that in one step, but you can first insert and then update...
One possibility is to expose a stored procedure, and when the Stuff parameter is null, update the insert with the LAST_INSERT_ID(), otherwise pass the non-null value to the insert.
Try this:
INSERT INTO `TABLE` (`ID`,`Stuff`) VALUES(NULL, `ID`);
I need two id columns in the same table that create unique values upon insert. Since MySQL allows only one column to auto-increment, I need to do something like max(id)+1 for the other column.
Here's the SQL I expected to work:
INSERT INTO invoices (invoiceid)
VALUES ((SELECT MAX(invoiceid)+1 FROM invoices))
The select statement works independently, but within my INSERT, it's not allowed. I get the error : You can't specify target table 'invoices' for update in FROM clause
You want to use INSERT INTO .... SELECT FROM instead of INSERT INTO...VALUES():
INSERT INTO invoices (invoiceid)
SELECT MAX(invoiceid)+1
FROM invoices
My question for you would be why are you not use an AUTO INCREMENT field to generate the invoiceid value? That is what it is for, then you will not have to create this when inserting data.
Hi I would like to copy entire contents from column Item under table IName to column Name under table Item both belonging to the same database.
I am giving the following query but it throws the error saying that the subquery returned more than one records. (There are around 600 records)
Insert into Item set name = (Select Item from IName)
Thanks
INSERT INTO Item (Name)
SELECT Item
FROM IName
When you want to insert into a single-column* table, INSERT works either with:
INSERT INTO table (column)
VALUES (value1),(value2), ... (valueN) ;
or with:
INSERT INTO table (column)
SELECT a_column
FROM a_table
--- optional (multiple) JOINs
--- and WHERE
--- and GROUP BY
--- any complex SELECT query
(OK, the above can work with a multiple-column table, too, as long as all the other - not explicitely stated in the INSERT statement - columns have been defined with a DEFAULT value or with AUTO_INCREMENT.)
The INSERT ... SET syntax is valid in MySQL only and can be used only when you want to insert one row exactly:
INSERT INTO table
SET column = value1 ;
is equivalent to:
INSERT INTO table (column)
VALUES (value1) ;
INSERT INTO Item (name)
SELECT Item FROM IName
Link
INSERT INTO table_one (column1) SELECT column2 FROM table_two
See MySQL Ref
I have a table 'attendance' having 3 fields 'class_total','class_attended' and 'attendance_percent'.
I want to insert/update the value of 'attendance_percent' whenever values are inserted/updated in the fields 'class_total' and 'class_attended' by (class_attended/class_total)*100.
For this I am using the trigger:
CREATE TRIGGER percent_update
BEFORE INSERT ON attendance
FOR EACH ROW
SET NEW.attendance_percent =(attendance.class_attended/attendance.class_total)*100 ;
But its not working.
Use a NEW clause instead of table name -
...
SET NEW.attendance_percent = (NEW.class_attended/NEW.class_total)
...