I have a query like this:
INSERT INTO table1(col1,col2,col4)
VALUES (1, (select col1 from table2 where col2 = :param), 1);
The above query works as well. Now I want to use two columns from table2, like this:
INSERT INTO table1(col1,col2,col3,col4)
VALUES (1, (select col1,col2 from table2 where col2 = :param), 1);
But second query doesn't work, How can I fix it?
INSERT INTO table1(col1, col2, col3, col4)
select 1, col1, col2, 1
from table2
where col2 = :param;
Related
I am trying to insert a text with a value from another table, can it be done in one query?
if I remove ('value from table2 : ' +) from the query, the value selected is inserted properly, but when I add it, the value inserted is 0.
INSERT INTO table1 ( col1, col2, col3, col4)
VALUES ( 896, 'azer', 'value from table2 : ' + (SELECT table2.col2 FROM table2 WHERE table2.col1 = 5 ), 675)
the inserted value in col3 should like this: "value from table2 : zsqd"
You need to use CONCAT function for concatenation.
INSERT INTO table1 ( col1, col2, col3, col4)
VALUES ( 896, 'azer', (SELECT CONCAT('value from table2 :',table2.col2) FROM table2 WHERE table2.col1 = 5 ), 675)
Very simple
If col3 in table1 is varchar or text then try this
INSERT INTO table1 ( col1, col2, col3, col4)
VALUES ( 896, 'azer', (SELECT CONCAT("value from table2 : ",table2.col2) FROM table2 WHERE table2.col1 = 5 ), 675)
I understand that LIKE can not be used in inside IN clause from here.
My table has col1, col2, col3 columns.
How can I find multiple values in multiple columns using LIKE operator.
Currently my query is:
SELECT col1, col2, col3
FROM table_name
WHERE
(
Col1 = '38'
OR Col1 LIKE '%,38'
OR Col1 LIKE '%,38,%'
OR Col1 LIKE '38,%'
)
OR
(
col2 = '38'
OR col2 LIKE '%,38'
OR col2 LIKE '%,38,%'
OR col2 LIKE '38,%'
)
OR
(
col3 = '38'
OR col3 LIKE '%,38'
OR col3 LIKE '%,38,%'
OR col3 LIKE '38,%'
)
Is there a way make it smarter/shorter/faster?
Thanks.!
You can use the function FIND_IN_SET(). MySQL has a dedicated function FIND_IN_SET() that returns field index if the value is found in a string containing comma-separated values.
Example
DROP TABLE table_name;
CREATE TABLE table_name
(col1 VARCHAR(100));
INSERT INTO table_name VALUES ('38');
INSERT INTO table_name VALUES ('37,38,39');
INSERT INTO table_name VALUES ('37,38');
INSERT INTO table_name VALUES ('38,39');
INSERT INTO table_name VALUES ('48');
SELECT col1
FROM table_name
WHERE FIND_IN_SET(38, col1) > 0;
Your solution would be
SELECT col1, col2, col3
FROM table_name
WHERE FIND_IN_SET(38, col1) > 0
OR FIND_IN_SET(38, col2) > 0
OR FIND_IN_SET(38, col3) > 0 ;
I have a table in MySQL as below:
ID, COL1, COL2 VALUE
'1', 'OBJ1', 'OBJ2', '5'
'2', 'OBJ1', 'OBJ2', '1'
'3', 'OBJ2', 'OBJ1', '3'
'4', 'OBJ3', 'OBJ1', '4'
'5', 'OBJ3', 'OBJ4', '6'
Relation between col1 and col2 is independent of position, ie OBJ1 in col1 and OBJ2 in col2 is same as OBJ1 in col2 and OBJ2 in col1. This means that OBJ1 and OBJ2 shares a relationship.
Now, this means that the object OBJ1 and OBJ2 have a value of 1,5,3...
I want to keep only distinct values ie OBJ1, OBJ2 should occur only once in the table, not even OBJ2,OBJ1.
Importantly, I want to retain only the row with HIGHEST value.
The result I want is thus:
ID, COL1, COL2 VALUE
'1', 'OBJ1', 'OBJ2', '5'
'4', 'OBJ3', 'OBJ1', '4'
'5', 'OBJ3', 'OBJ4', '6'
What is the best and efficient way of doing this? I have over 10 million rows.
I have searched in many forums/Google but cannot find the exact answer I am looking for..
Try this:
SELECT t1.ID, t1.COL1, t1.COL2, t1.VALUE
FROM mytable AS t1
JOIN (
SELECT LEAST(COL1, COL2) AS C1,
GREATEST(COL1, COL2) AS C2,
MAX(VALUE) AS max_Value
FROM mytable
GROUP BY LEAST(COL1, COL2),
GREATEST(COL1, COL2)
) AS t2 ON t1.COL1 = t1.C1 AND t1.COL2 = t2.C2 AND t1.VLAUE = t2.max_Value
You could use an in clause and subselect grouped by
for solve also the problem related to the distinct pair combination
You should organize the data in a proper way
select
id
, case when col1 <= col2 then col1 else col2 end COL1
, case when col1 > col2 then col1 else col2 end COL2
, value
from start_table
then the query became
SELECT t1.ID, t1.COL1, t1.COL2, t1.VALUE
FROM (
select
id
, case when col1 <= col2 then col1 else col2 end COL1
, case when col1 > col2 then col1 else col2 end COL2
, value
from start_table
) t1
where value in (
select max(value)
FROM (
select
id
, case when col1 <= col2 then col1 else col2 end COL1
, case when col1 > col2 then col1 else col2 end COL2
, value
from start_table
) mytable
group by col1, col2
)
or using an inner join
SELECT t1.ID, t1.COL1, t1.COL2, t1.VALUE
FROM (
select
id
, case when col1 <= col2 then col1 else col2 end COL1
, case when col1 > col2 then col1 else col2 end COL2
, value
from start_table
) t1
inner join
(
select max(value) as value
FROM (
select
id
, case when col1 <= col2 then col1 else col2 end COL1
, case when col1 > col2 then col1 else col2 end COL2
, value
from start_table
) mytable
group by col1, col2
) T2 on t1.value = t2.value
Rebuild the table so that no dups are allowed; in the process, get rid of the dups. (And get rid of the apparently useless id.)
CREATE TABLE new (
col1 ...,
col2 ...,
`value` ...,
PRIMARY KEY(col1, col2),
INDEX(col2, col2, `value`)
) ENGINE=InnoDB;
INSERT INTO new (col1, col2, `value`)
SELECT LEAST(col1, col2),
GREATEST(col1, col2),
`value`
ON DUPLICATE KEY UPDATE
`value` := GREATEST(`value`, VALUES(`value`));
RENAME TABLE real TO old,
new TO real;
DROP TABLE old;
In the future, you will need this for INSERTing/UPDATEing new rows:
INSERT INTO new (col1, col2, `value`)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE
`value` := GREATEST(`value`, VALUES(`value`));
(This assumes you want to increase value whenever it is already in the table.)
These save space and speed (important for 10M rows): Getting rid of id; having optimal indexes; using InnoDB; etc.
I'm trying to insert into a table with the following syntax:
INSERT INTO table1(
col1, col2, col3)
SELECT distinct
col1, col2, getDate()
FROM table2 WHERE NOT EXISTS(
SELECT 1 FROM table1, table2
WHERE ((table1.col1 = table2.col1) or (table1.col1 is null and table2.col1 is null))
AND ((table1.col2 = table2.col2) or (table1.col2 is null and table2.col2 is null)))
But when I run the query, it shows (0 row(s) affected).
The SELECT statement within the NOT EXISTS statement returns the correct number of rows that I don't want inserted. If I try to insert into the table without the WHERE NOT EXISTS statement, it inserts everything. I only want to insert rows that are not already in table1.
Try this:
INSERT INTO table1(col1, col2, col3)
SELECT distinct col1, col2, getDate()
FROM table2 WHERE NOT EXISTS(
SELECT 1 FROM table1
WHERE ((table1.col1 = table2.col1) or (table1.col1 is null and table2.col1 is null))
AND ((table1.col2 = table2.col2) or (table1.col2 is null and table2.col2 is null)))
You can optimize this query quite a bit, but as a quick fix you could change:
SELECT 1 FROM table1, table2
to:
SELECT 1 FROM table1
This will tie the outer table2 into your subquery.
We have 2 columns in a table and want to SELECT the lesser most of the two (unless it is equal to 0, then we want the nonzero column).
Is there a way to do this while keeping it a simple SELECT statement?
What we want is this:
SELECT LEAST(col1, col2) FROM myTable
Unless col2 is 0, in which case we want col1.
SELECT LEAST(
IF(col1, col1, col2),
IF(col2, col2, col1)
)
FROM myTable
SELECT
CASE
WHEN col1 = 0 THEN col2
WHEN col2 = 0 THEN col1
ELSE LEAST(col1, col2)
END AS MinCol
FROM myTable