I have 1 table
customer_assignments having customer_id, and category_id columns.
I want to insert multiple category _id's like 1,2,3 by customer_id=62 at a time ..the table look's like
customer_id category_id
62 1
62 2
62 3
suppose if I update category _ids like 1,2 by customer_id=62, the table look's like
customer_id category_id
62 1
62 2
insert and update both will occur at a time....am new to mysql
I need query...
use 2 queries
1.
DELETE * FROM customer_assignments
2.
then now insert waht ever u want using INSERT query
Related
I have a table cities like this:
id
city
1
Vancouver
2
Calgary
3
Calgry
And multiple other tables which reference cities, something like this (just some example numbers).
id
city_id
year
population
1
1
2000
100000
2
1
2001
130000
3
3
2000
70000
4
3
2001
85000
5
2
2002
95000
I want to merge/consolidate city id 3 into id 2, so change every occurrence of id 3 across all tables to 2, and then delete city id 3. Is there a clean way of doing this?
Something like ON UPDATE CASCADE would work perfectly but I can't have duplicate primary ids. At the very least I could loop through the foreign keys and run a query on every table but I'm not sure if there's a more idiomatic way.
ON UPDATE CASCADE causes duplicate PK. To avoid that, we can use ON DELETE SET NULL which nullify all the city_id with a value 3 in other tables once we delete the city_id 3 from the cities table, but then you would still have to change the city_id from null to 2 in the said tables. This is not fundamentally different to change those city_id with a value 3 to 2 for the other tables , then just delete id 3 from the cities table. If you feel reluctant to manually do that or if there are too many tables to handle, then use a procedure. Make a list of table , declare a cursor for the list and loop through the list to get each table name and use a prepared statement to do the UPDATE.
I am wondering if any of you would be able to help me. I am trying to loop through table 1 (which has duplicate values of the plant codes) and based on the unique plant codes, create a new record for the two other tables. For each unique Plant code I want to create a new row in the other two tables and regarding the non unique PtypeID I link any one of the PTypeID's for all inserts it doesnt matter which I choose and for the rest of the fields like name etc. I would like to set those myself, I am just stuck on the logic of how to insert based on looping through a certain table and adding to another. So here is the data:
Table 1
PlantCode PlantID PTypeID
MEX 1 10
USA 2 11
USA 2 12
AUS 3 13
CHL 4 14
Table 2
PTypeID PtypeName PRID
123 Supplier 1
23 General 2
45 Customer 3
90 Broker 4
90 Broker 5
Table 3
PCreatedDate PRID PRName
2005-03-21 14:44:27.157 1 Classification
2005-03-29 00:00:00.000 2 Follow Up
2005-04-13 09:27:17.720 3 Step 1
2005-04-13 10:31:37.680 4 Step 2
2005-04-13 10:32:17.663 5 General Process
Any help at all would be greatly appreciated
I'm unclear on what relationship there is between Table 1 and either of the other two, so this is going to be a bit general.
First, there are two options and both require a select statement to get the unique values of PlantCode out of table1, along with one of the PTypeId's associated with it, so let's do that:
select PlantCode, min(PTypeId)
from table1
group by PlantCode;
This gets the lowest valued PTypeId associated with the PlantCode. You could use max(PTypeId) instead which gets the highest value if you wanted: for 'USA' min will give you 11 and max will give you 12.
Having selected that data you can either write some code (C#, C++, java, whatever) to read through the results row by row and insert new data into table2 and table3. I'm not going to show that, but I'll show how the do it using pure SQL.
insert into table2 (PTypeId, PTypeName, PRID)
select PTypeId, 'YourChoiceOfName', 24 -- set PRID to 24 for all
from
(
select PlantCode, min(PTypeId) as PTypeId
from table1
group by PlantCode
) x;
and follow that with a similar insert.... select... for table3.
Hope that helps.
I have this sql table with the name oc_product_to_category
example of sql table.
product_id category_id
1 146
2 146
.. 146
16 147
17 147
... ...
1800 191
So my question is how i can copy table and change and change only the category_id?
So my result need to be like this.
example of sql table nedded result
product_id category_id
1 146
1 200
2 146
2 200
.. 146
.. 200
16 147
16 260
17 147
17 260
... ...
1800 191
1800 291
Sorry for bad explanation but i cant import image.
INSERT INTO oc_product_to_category ( category_id) SELECT "147" FROM oc_product_to_category WHERE category_id = 146
I was traying with somtheihng like this but i get error Duplicate enrty 147 - 0 for key PRIMARY
Create an exact copy including indexes of your table with this:
CREATE TABLE new_table LIKE old_table;
Then insert with
INSERT INTO new_table
SELECT whatever_the_logic_is_for_your_new_data FROM old_table;
If you don't want to include indexes and foreign keys constraints and what not, you can also do it like this:
CREATE TABLE new_table AS
SELECT whatever_the_logic_is_for_your_new_data FROM old_table;
For copying table you can use the following query :
Select * Into [NewTable] From oc_product_to_category
then you have to update the table but I dont know exactly where number 200 and 260 come from
If they are already in table so you dont have to update just add an Order by clause in your select from that table , if they are not in the table then you have to add.
please be more specific what you are going to update
I have a table A
code_Value key_value Description
12 12 Entry_Category5
13 rrtt Entry2
20 tht Entry6
20 trt Wntry9
Table A has similar ways million records..
A logic is implemented in Table B which uses Table A as source
Code_value Key_value Description
12 12 Entry_Category5
13 rrtt Entry2
13 13 Null value
20 tht Entry6
20 trt Entry9
20 20 Null value
The logic is, in table A if i have an entry, where code is not equal to key then a new entry of my previous code will be copied with key as the code,description must be null.
This logic must be applied to million records.I just want to have an sql query which will
help me.Please suggest since there are more records
Try:
insert into table_b
select
code_value,key_value,description
from
table_a
where
code_value = key_value
union
select
code_value,code_value,null
from
table_a
where
code_value != key_value
I have table like this:
id products
------ ----------
5 1,2,3
6 2,4,5
9 1,4,7
17 4,6,7
18 1,6,8
19 2,3,6
I have to select only that rows, which row's products column contains one of (2,3) values.
In this case query must return:
id products
------ ----------
5 1,2,3
6 2,4,5
19 2,3,6
But I don't understand how to make construction of this query.
Any ideas?
Thanks in advance.
SELECT id,products
FROM yourTable
WHERE FIND_IN_SET('2',products)>0
OR FIND_IN_SET('3',products)>0
sqlFiddle
Would you mind to try this one please?
select * from TABLE_NAME where products regexp "(^|,)[23](,|$)";
Its doing either two or three at the begining, or at end. Or in between the commas.
Never, never, never store multiple values in one column.
Like you see now this will only give you headaches. Normalize your table. Then you can select normally.
Your table should look like this
id product
-- -------
5 1
5 2
5 3
6 2
6 4
6 5
...
With that table structure your select would be
select id
from your_normalized_table
where product in (2,3)
group by id
having count(distinct product) = 2
That query can make use of indexes and is really fast.