Duplicate all rows and change but change store_id - mysql

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

Related

Updating all columns of a table from another table's data?

What I want to do is simple but I lack some sql knowledge, so I would truly appreciate any kind of help.
I have table 'dev'.
product1 | product2 | count
---------------------
I also have a table likes.
wishlist_id | user_id | product_id
-------------------------------
1 2 54
2 2 60
3 3 54
7 3 60
.. .. ..
99 99 99
I want to find how many times 2 products where liked together, but instead of creating a view I want to save the data in the dev table.
How can I update the rows so dev table becomes:
dev
-----------------------
product1 product2 count
54 60 2
..
EDIT
I don't know how to actually use the update statement here.
my query is :
SELECT i1.product_id as product1, i2.product_id as product2,
COUNT(*) as num
FROM likes i1
INNER JOIN likes i2 ON i1.wishlist_id = i2.wishlist_id
AND i1.product_id < i2.product_id
GROUP BY product1, product2;
Suppose your dev table has a unique key constraint on the pair of product1, product2:
CREATE TABLE dev (
product1 INT NOT NULL,
product2 INT NOT NULL,
count INT NOT NULL DEFAULT 0,
PRIMARY KEY (product1, product2)
);
Now you can use INSERT...ON DUPLICATE KEY UPDATE to add or replace rows corresponding to pairs of your products. You already had a SELECT statement that generated the rows, now you need to use it in an IODKU.
INSERT INTO dev (product1, product2, count)
SELECT i1.product_id as product1, i2.product_id as product2, COUNT(*) as num
FROM likes i1 INNER JOIN likes i2
ON i1.wishlist_id = i2.wishlist_id AND i1.product_id < i2.product_id
GROUP BY product1, product2
ON DUPLICATE KEY UPDATE count = VALUES(count);
Read more about IODKU here: https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html

Insert into table if second table has conditional value

I have two tables:
Table 1 (customers)
customer_id customer_name salesPerson_id
1 John 1
2 Ed 1
3 Sam 2
Table 2 (customerContacts)
contact_id customer_id phone_number
1 1 687-5309
2 1 555-1234
3 1 742-1111
I am trying to let only the sales person add / update a phone number for their specific customer.
So only sales salesPerson_id 1 could update John and Ed and only salesPerson_id 2 could update Sam.
I believe I am looking for something like:
INSERT INTO customerContacts (contact_id , customer_id , phone_number) VALUES (1 , 1 , '987-6543')
ON DUPLICATE KEY UPDATE phone_number='987-6543'
IF customers.salesPerson_ID = 1
But it doesn't seem like sql supports if statements.
INSERT INTO customerContacts (contact_id , customer_id , phone_number)
Select 1 , customer_id , '987-6543'
from customers
where salesPerson_ID = 1 and customer_id=1;
This is the query which you should use in the native way but you need to fit this in your application framework

what is an efficient query for this task

I have a data file that looks like this
item_id | status
1 | null
2 | null
2 | new
3 | new
4 | null
4 | new
5 | null
Notice that item 2 and 4 have both 2 status: null and new.
I want to create a query that would extract only item_id with 1 status, which is null. So, i want my query to extract only 1 and 5.
I ended up doing this, but this doesn't look efficient:
1.List items with null status
create table query_1 as
select * from table1 where status = 'null';
2.List item with new status
create table query_2 as
select * from table1 where status = 'new';
3.select all results from query 1, excluding any id found from the results of query 2
select * from query_1 where item_id not in (select item_id from query_2)
Am I over thinking this? Is there a simpler query that can accomplish this?
First you have to check for null values with IS NULL. =null or ='null' won't work.
SELECT item_id, MAX(status)
FROM table1
GROUP BY item_id
HAVING MAX(status) IS NULL
select item_id
from table1
group by item_id
having count(*) = 1 and status is null
You can use self-join. What you need to do is remove the item_id's having new values. The remaining entries would be would you want. So you can frame your query as such :
SELECT item_id,
status
FROM tableName
WHERE item_id NOT IN (SELECT item_id
FROM #tab
WHERE status = 'new')
You can see this here -> SQL Fiddle Example
SELECT item_id FROM items
WHERE status IS NOT NULL
AND item_id NOT IN
(SELECT item_id FROM items
WHERE status IS NULL
)
You could use DISTINCT in this instance
SELECT DISTINCT item_id FROM items WHERE status IS NULL;

MySQL - Column already exists: 1060 Duplicate column name '1'

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

How to get ID for INSERT if name already used, else generate new one

I'm having a bit of trouble with an INSERT query.
I have a table I'm inserting a value into that's like this:
TABLE cars
ID Brand Model B_ID
---------------------------
1 Ford Escort 1
2 Ford Focus 1
3 Nissan Micra 2
4 Renault Megane 3
5 Ford Mustang 1
ID is unique and B_ID is the same ID for every same brand.
When inserting a new entry I want to be able to check if a brand is already in there and use that same B_ID otherwise I want to increment the highest B_ID and insert that.
I've got this far:
INSERT INTO 'cars' ('brand', 'model', 'B_ID')
VALUES (
'Nissan'
'Note'
'SELECT B_ID FROM cars WHERE brand = 'Nissan'
)
How can I get the highest B_ID and increment it by one if there is no match with my subquery because it's a new brand?
I'm using MySQL.
INSERT INTO `cars` (`brand`, `model`, `B_ID`)
select 'Nissan', 'Note', coalesce(Max(B_ID),0)+1 FROM cars WHERE brand = 'Nissan'
Until you normalize your tables:
INSERT INTO cars
(brand, model, B_ID)
SELECT 'Nissan'
, 'Note'
, COALESCE( ( SELECT B_ID
FROM cars
WHERE brand = 'Nissan'
LIMIT 1
)
, ( SELECT MAX(B_ID)
FROM cars
) + 1
, 1 --- this is for the case when the table is empty
)
Also notice that if you have multiple concurrent INSERT, you may end with rows that have different brand but same B_ID.