I've been trying to make a INSERT with unique rows, however, if the unique row already exist, it will simply ignore the insert and return no error.
Why, and/or what would be wrong with the query?
INSERT INTO hashtag_mapping (user_id, cid, hashtag_id, date_created, date_modified)
SELECT * FROM (SELECT 1, 8923, 1, NOW(), CURRENT_TIMESTAMP) AS tmp
WHERE NOT EXISTS (
SELECT user_id, cid, hashtag_id
FROM hashtag_mapping
WHERE user_id = 1
AND cid = 8923
AND hashtag_id = 1
) LIMIT 1;
The unique key: unique_mapping (user_id, cid, hashtag_id), Unique
The following error that I receive from MySQL:
Column already exists: 1060 Duplicate column name '1'
And the table design if it's helps...
id user_id cid hashtag_id date_created date_modified
------ ------- ---------- ---------- ------------------- ---------------------
1 1 8644 1 2016-03-23 15:19:54 2016-04-06 11:39:32
2 1 8644 2 2016-03-23 15:19:54 2016-04-06 11:39:34
3 1 8664 3 2016-03-25 17:02:32 2016-04-06 11:39:35
4 1 8664 4 2016-03-25 17:02:32 2016-04-06 11:39:36
You must give a alias for the columns. if you dont MySQL will take the Constant as name.
SELECT 1 AS field1 , 8923 AS something , 1 AS field2, NOW(), CURRENT_TIMESTAMP
Related
user
user_id | name
1 | John
2 | Matt
food
food_id | food_name
1 | A
2 | B
fav_food
user_id(-> user) | food_id(->food)
2 | 1
My insertion query:
INSERT INTO `fav_food`(user_id,food_id) VALUES(?,(SELECT id from `food` where food_name=?))
When the SELECT id from food where food_name=? subquery returns null insertion fails with an error which should be.
My question is, How can I ignore the insertion only when the subquery returns null or no rows? Thanks
With EXISTS:
INSERT INTO fav_food (user_id, food_id)
SELECT ?, (SELECT food_id FROM food WHERE food_name = ?)
WHERE EXISTS (SELECT 1 FROM food WHERE food_name = ?)
See the demo
You can try this:
INSERT INTO `fav_food`(user_id,food_id) VALUES(?,(SELECT id from `food` where food_name=? AND food_name IS NOT NULL))
From what I've tried before, you can do that by using an if statement in a stored procedure, the code might look something like the following:
if exists (SELECT id from `food` where food_name=?) then
INSERT INTO `fav_food`(user_id,food_id) VALUES(?,(SELECT id from `food` where food_name=?))
end if;
I have this table name: copy_stores
copy_id | store_id
11221 2
11222 2
112223 2
there is about 2000 records, but I like to duplicate all the records but on the newly duplicated change the store_id to 1 where it's 2
I have tried this, but it won't work:
insert into copy_stores(`copy_id`, `store_id`)
SELECT 1, `copy_id`, `store_id`
from copy_stores
where store_id = 2
You want '1' to be the new store_id, so need to select it after the copy_id:
INSERT INTO copy_stores(copy_id, store_id)
SELECT copy_id, 1
FROM review_store
WHERE store_id = 2
I Have this cat id - post id relation table.
+----+--------+---------+
| id | cat_id | post_id |
| | | |
| 1 | 11 | 32 |
| 2 | ... | ... |
+----+--------+---------+
I use SELECT WHERE cat_id = 11 AND post_id = 32 and then if no result found, I do INSERT.
Can I rewrite these two queries in One?
You can do something like this:
insert into cats_rel(cat_id, post_id)
select 11, 32
where not exists (select 1 from cats_rel where cat_id = 11 and post_id = 32);
EDIT:
Oops. That above doesn't work in MySQL because it is missing a from clause (works in many other databases, though). In any case, I usually write this putting the values in a subquery, so they only appear in the query once:
insert into cats_rel(cat_id, post_id)
select toinsert.cat_id, toinsert.post_id
from (select 11 as cat_id, 32 as post_id) toinsert
where not exists (select 1
from cats_rel cr
where cr.cat_id = toinsert.cat_id and cr.post_id = toinsert.post_id
);
You can use Replace
REPLACE INTO 'yourtable'
SET `cat_id` = 11, `post_id` = 32;
if the record exists it will overwrite it otherwise it will be created;
Update :
For this to work you should add a unique key to the pair of columns not only one
ALTER TABLE yourtable ADD UNIQUE INDEX cat_post_unique (cat_id, post_id);
We can use "from dual" clause for MySQL:
insert into cats_rel(cat_id, post_id)
select 11, 32 from dual
where not exists (select 1 from cats_rel where cat_id = 11 and post_id = 32);
Given the following tables:
Topic
id, last_updated_child_id
Response
id, topic_id, updated_at
How do I update the Topic table so the last_updated_child_id is equal to the latest response id (based on date).
So for example given:
Topic
id last_updated_child_id
-- -----------------------
1 null
2 null
3 null
Response
id topic_id updated_at
-- ---- ----
1 1 2010
2 1 2012
3 1 2011
4 2 2000
I would like to execute an UPDATE statement that would result in the Topic table being:
id last_updated_child_id
-- -----------------------
1 2
2 4
3 null
Note: I would like to avoid temp tables if possible and am happy for a MySQL specific solution.
Not very efficient, but relatively simple:
UPDATE topic
SET last_id = (SELECT id
FROM response
WHERE topic_id = topic.id
ORDER BY updated_at DESC
LIMIT 1);
I'm looking to create a sql statement that will update a large set of data.
What I have is a table like
id, transid, amount, narative1, narative 2, total, active
1 1234 23.2 NULL NULL NULL 1
2 1234 120.33 NULL NULL NULL 1
3 1235 98.00 NULL NULL NULL 1
When there are two rows with the same transid I need to total them put the result in the total column of the first one with that transid and put the second amount in naritive2 of the first instance as well as make the second one inactive. It should ignore single rows for a transid.
The result of what I want to do should be:
id, transid, amount, narative1, narative 2, total, active
1 1234 23.2 NULL 120.33 143.53 1
2 1234 120.33 NULL NULL NULL 0
3 1235 98.00 NULL NULL NULL 1
I know a bit of a thong twister but..
Ideally I'd like to do this in just a MySQL statements. So I don't mind having to do multiple sql statements but I want to avoid connecting it to PHP etc. Its a very large set of data.
This will update only those transactions that have exactly 2 rows (not 1 and not 3 or more).
UPDATE mytable mtu
JOIN (
SELECT minid, maxid, mtmin.amount AS minamt, mtmax.amount AS maxamt
FROM (
SELECT MIN(id) AS minid, MAX(id) AS maxid
FROM mytable mti
GROUP BY
transid
HAVING COUNT(*) = 2
) mt
JOIN mytable mtmin
ON mtmin.id = minid
JOIN mytable mtmax
ON mtmax.id = maxid
) mts
ON id IN (minid, maxid)
SET narative2 = CASE id WHEN minid THEN minamt ELSE NULL END,
total = CASE id WHEN minid THEN minamt + maxamt ELSE NULL END,
active = (id = minid)