I want to Insert multiple value if <input id="data3" exist. but when there's didn't exist. I want to Insert single row value.
example :
Insert into table (col1,col2,col3)
values ('a','b,'c1'), ('a','b','c2');
Inside database showing
col1 | col2 | col3
a b c1
a b c2
Then if col3 on second row (c2) does not exist. the database still inserting data like this :
col1 | col2 |col3
a b c1
a b
so how to insert only single row value like this :
col1 | col2 |col3
a b c1
Related
I'm doing trigger and depending on data inserted, I want to compare the input of data to select.
I have 3 columns col1, col2 and col3 (only one of them can be not null). When I insert data to table, I want to check if data joined to that column is correct.
I tried CASE but it didn't work.
CASE
WHEN col1 IS NOT NULL THEN SELECT * FROM XXX
WHEN col2 IS NOT NULL THEN SELECT * FROM YYY
WHEN col3 IS NOT NULL THEN SELECT * FROM ZZZ LEFT JOIN aaa ON col3.id=aaa.id
ELSE NULL
END
Table mytable in mysql has columns col1 and col2 with integer not null values. It also has a primary key id. After arranging the columns in a certain order, I need to calculate a certain sum from all the elements of columns col1 and col2 as follows: For a given value in col2 in a given row, compare it with the value of col1 in the next row. Take the smaller of these two values and subtract from it the value of col1 in the row where the value of col2 was taken; for the last row just subtract from the value in col2 the value in col1 in that row. Get the sum of all these differences.
I have the following solution which seems to work but might disturb where the user lacks privileges to create tables or alter the table structure. It would require checking existence of a table and some columns before they are created, besides needing to clean up after running, to remove the columns added.
alter table mytable add my_id int(5) not null;
alter table mytable add col3 int(7) not null;
SET #count = 0;
UPDATE mytable SET my_id = #count:= #count + 1;
create temporary table mytable2 as select my_id, col1 from mytable limit 1, 99999;
update mytable a join mytable2 b on a.my_id = (b.my_id - 1) set a.col3 = b.col1;
update mytable set col3 = col2 order by my_id desc limit 1;
select sum(LEAST(col2,col3) - col1) from mytable;
The Question: What would be a better way of getting the desired sum described above?
Here is some sample data:
id col1 col2
--------------------
11 609 618
42 620 628
23 624 630
34 627 637
Required Sum: (618-609) + (624-620) + (627-624) + (637-627) = 26
I have a table T1:
Col1 | Col2
-------------
a | 2
b | 3
If Col1 is unique and Col2 is not then I can run the query:
INSERT INTO T1 (Col1, Col2)
VALUES (b,2)
ON DUPLICATE KEY UPDATE
Col2=VALUES(Col2);
and the table now looks like this:
Col1 | Col2
-------------
a | 2
b | 2
If Col1 and Col2 are both unique I would like to run a single query:
INSERT INTO T1 (Col1, Col2)
VALUES (b,2)
ON DUPLICATE KEY -- REPLACE ALL INSTANCES ?
Col1 = VALUES(Col1), Col2=VALUES(Col2);
which results in this:
Col1 | Col2
-------------
b | 2
So the key "b" is now associated with the key "2", replacing both "a"'s association and "b"'s previous association.
Is there a mysql query or extension that can accomplish this? (Clearly this can be done with multiple queries, I'm looking for an analogue of the ON DUPLICATE extension for this use case.)
After looking around for a while, I'm pretty certain that this cannot be done. There is no mention of anything other than UPDATE or IGNORE for ON DUPLICATE KEY in the manual. The requested action is tantamount to doing a DELETE inside an UPDATE statement. (Which you cannot do).
The following code performs the task, I don't like having to wrap it in a transaction but it is the only way to do it at the moment.
START TRANSACTION;
DELETE FROM t1 WHERE Col2 = 2 AND Col1 <> "b"; -- Without checking Col1 the row will be replaced instead of updated if the entry ("b",2) already exists
INSERT INTO t1 (Col1, Col2)
VALUES("b",2)
ON DUPLICATE KEY UPDATE
Col2 = VALUES(Col2);
COMMIT;
Using MySQL while retrieving, and processing a data file, I m trying to see records from various columns in one row for an ID so that it ignore null values of other columns and show all values in a single row
Columns col1, col2, col3, col4 with null values for a unique row ID
For this retrieved data table
col1 col2 col3 col4
row1 1 null null null
row2 null 2 null null
row3 null null 3 null
row4 null null null level1
finalRow 1 2 3 level1
Select ID, IFNULL(col1, col2, col3, col4 From table t
Brings error message. MAX function doesn't work for me as at time MIN values need to be get pickup as well.
if I understand right, this might be a solution:
Select ID, IFNULL(col1, IFNULL(col2, IFNULL(col3, col4))) from ...
Given your example MAX will work. In your example there is only one non-null value per column so the maximum value is also the minimum. So, this will get your expected result... which I think is the finalRow:
select max(col1), max(col2), max(col3), max(col4) from table
It would be helpful to provide a better explanation of what you're looking for and also adding more examples.
Let's say I have two tables of similar structure, but one is empty and the other has a few rows of information:
table1
col1 | col2 | col3
red | cow | 1
blue | dog | 2
gray | pig | 3
table2
col1 | col2 | col3 | col4
table3
col1 | col2
Attempting:
insert into `table2` select * from `table1`
will not work because of the unmatched column count, and the same is true substituting table3 for table2.
Altering the * portion of the SELECT statement is not an option for dynamic purposes. So a workaround would have to be a modified SELECT combining the information.
Is there a JOIN statement or something that would merge the structures of the tables and the data so it would look like this:
select * from `table1`,`table2` (JOIN or some other statement)
col1 | col2 | col3 | col4
red | cow | 1 | NULL
blue | dog | 2 | NULL
gray | pig | 3 | NULL
select * from `table1`,`table3` (JOIN or some other statement)
col1 | col2
red | cow
blue | dog
gray | pig
Basically just merging any columns with identical names and nullifying outside matches. Again, it can't refer to specific column names to keep things dynamic. It seems doable, but I'm finding it impossible to find the answer.
Thanks a ton to anybody that can help.
If the target table of the INSERT is a subset of the source table like in your example, then there is a simple solution:
insert into table2(col1, col2, col3)
select *
from table1;
Or, if the first columns of both tables are in the same order like in your example, then you your posted query should actually work:
insert into table2 select * from table1;
I quote the manual for version 5.6 on that:
If you are not running in strict SQL mode, any column not explicitly
given a value is set to its default (explicit or implicit) value. For
example, if you specify a column list that does not name all the
columns in the table, unnamed columns are set to their default values.
Default value assignment is described in Section 10.1.4, “Data Type
Default Values”. See also Section 1.8.6.2, “Constraints on Invalid
Data”.
However, it is almost always a bad idea to depend on "SELECT *" in an INSERT operation. So, in spite of your request, it really should be:
insert into table2(col1, col2, col3)
select col1, col2, col3
from table1;
For anything more you'll have to use DESCRIBE tablex and build your queries dynamically.
You could allow null values for the columns that will end up being excluded.
insert into `table2` (col1, col2, col3) select col1, col2, col3 from `table1`
may also be of use to you.