SQL copy and upadate table - mysql

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

Related

Is it possible to write an SQL query that returns the values of something that doesn't exist in another table?

I'm trying to write an SQL query that returns the rows of a table in which certain data of a column does not appear in another table.
For example, let's say I have two tables:
Table 1:
Date Name Room
2020/01/23 John 201
2020/01/22 Rebecca 203
2020/01/22 Ronald 204
2020/01/22 Jimmy 205
Table 2 (does NOT have the same amount of columns):
Date Room
2020/01/22 203
2020/01/23 201
2020/01/22 202
2020/01/22 209
I want to find all the rows in Table 2 in which the room number does NOT show in table 1. Which means my SQL would return
2020/01/22 202
2020/01/22 209
Since room 202 and room 209 does not appear in Table 1.
How would I do this?
Use not exists:
select t2.*
from table2 t2
where not exists (select 1
from table1 t1
where t2.room = t1.room
);

MYSQL select the date from one table that not exist in the second table

i have 2 tables
presence table:
matricule date_effet
248 2017-01-30
248 2017-01-31
248 2017-02-01
248 2017-02-02
Activities table :
Matricule date
248 2017-01-31
248 2017-02-01
248 2017-02-02
what i want is to extract the dates that exist on the first one and don't exist in the second one in this case 2017-01-30 knowing that the user will select a range date for exemple in this case maybe date between 2017-01-28 and 2017-02-02
try with
SELECT * FROM presence
MINUS
SELECT * FROM Activities
from SQL EXCEPT / MINUS
Try this:
SELECT date_effet FROM presence WHERE date_effet NOT IN(SELECT date FROM activities)
The inner SELECT statement will select all the dates from second table, and then the first select will select all the dates from first table which do not exist in the second table.
Hope it helps!

Oracle query issue in implementing logic for checking on million records

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

how to insert and update multiple values at a time

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

Store a set of numbers in mysql

I need to store a set of integers in MYSQL. The problem is as : I have an id(integer). Each id is mapped to a set of numbers. The numbers in the set can have values from 1 - 160. So my table should be of structure {id,set}. Also each id'set can have different length (max 40 numbers).
Eg:
id set
1 {2 45 46 67 89 155}
2 {1, 11 12 34 56 67 68 79 80 134 145}
I have tried the SET datatype in MYSQL. I defined it as
CREATE TABLE mytable ( id NUMBER PRIMARY KEY , mycol SET('1','2','3','4'...))
But the problem is the set allows to define only 64 elemnets.
Note: I need options other than creating a mapping table as :
id setno
1 2
1 45
....... and so on
Can anyone suggest another way to store a set of numbers in MYSQL.
Create child table to hold the numbers, one per row:
create table set_number (
set_id int,
num int
);
Then insert your numbers. To get them as one CSV value, use group_concat(), something like:
select t.id, group_concat(s.num) set
from mytable t
join set_nimber s
on t.set_id = s.set_id
group by t.id
The table structure you described is the right way to do.
And its all in the way you present data, create a table where it maps ID into Sento
And ID is a primary key
So that you will have:
ID Sento
1 2
1 45
1 46
...
1 155
And then have an SQL that says:
SELECT ID, GROUP_CONCAT(Sento, SEPARATOR ', ')
FROM Table
GROUP BY ID
Hope this helps