I have an existing MYSQL products table that has (amongst others) productID (autoinc) quantity productCode description
I have now been supplied with a spreadsheet that has all the products above + more new products.
I need to import only the new products that don't exist in the products table.
I am using sequel Pro on a mac. When I select the advanced section and select 'skip existing rows' it populates with productID = productID.
So I don't see a method to say if productCode = productCode skip this row.
I have created an new table productsAll that has all the new data in it and matched fields.
How do I loop through the productsAll table but only insert into the products table if the productCode does not exist.
Thanks..
You can use INSERT IGNORE
INSERT IGNORE INTO TABLE1 (SELECT * FROM TABLE2)
Related
So let's say I have a table Car and it has primary key ID, columns BrandID (ref to table Brand), Price, Comment and others.
.
The thing I need to do is to copy columns Price and Comment to the new table.
But also for every Car element I need to go to Brand table and get specific Brand Name depending on BrandID value and also copy it to the new table
How can I accomplish this via SQL script?
Create the new table direct from the SELECT statement with JOINed tbales
CREATE TABLE NEW_TABLE SELECT Price, Comment, name FROM car c INNER JOIN brand ON b.ID = c.BrandID
You can use
CREATE TABLE ... SELECT statement...
Take a look at this mysql document for details
https://dev.mysql.com/doc/refman/8.0/en/create-table-select.html
I have a column ProductName in Products table. User is able to duplicate the product. While duplicating the product I have to copy all fields from the Products table for the particular product and create new record except for ProductName field. The ProductName field should be as follows:
for 1st duplicate: ProductName_Copy_1
for 2nd duplicate: ProductName_Copy_2
etc
Can anyone suggest me how to do this.
Use a subquery to generate the number:
insert into product
select
concat(productname, '_',
(select count(*) from product
where productname like concat(p.productname, '%')),
col2, col3, ....
from product p
where id = <original product id to copy>
Well if you are using phpmyadmin, there is an auto_increment attribute you can set to rows. Each row will be uniquely identified by that value.
More information can be found here
http://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html
In my application database the table is called Customer which has fields:
CUSTOMER TABLE
cust_id
cust_name
cust_age
In above table cust_id data type is binary and UUID has been inserted into the table :
Insert statement looks like this:
INSERT INTO Customer VALUES ('444d264e-9024-11e4-968c-b82a72ad8ef8','swify',41);
The data has been inserted successfully . Similarly in this fashion i have inserted multiple records into the DB
Now i want to retrieve a particular record from the DB Usin select statement.
I tried this approach but it's giving no rows
SELECT * FROM Customer WHERE cust_id='444d264e-9024-11e4-968c-b82a72ad8ef8';
Hence, My question is how can i retrieve a record based on the UUID values;
Try this:
SELECT * FROM Customer WHERE cust_id LIKE '444d264e-9024-11e4-968c-b82a72ad8ef8';
I'm trying to update all the prices in one database with the prices from another where the product code matches (which isn't the primary key), whilst leaving the other fields columns untouched.
INSERT INTO inventory
SELECT * FROM temporary_table
ON DUPLICATE KEY UPDATE price = VALUES(price)
This is just duplicating the whole product where the code matches
Thanks in advance
Try using replace instead of insert. select all the columns from inventory table except the one you want to update(price)
REPLACE INTO inventory
SELECT b.col1, b.col2......... a.price
FROM temporary_table a, inventory b
where a.product_code = b.product_code
Basically I have a table that versions products,
so it has two columns of interest, id | product_id
id is an autoincrement column,
product_id is just an int
When a product is first created the product_id comes from the id,
When the product is edited we duplicate the row, so the product_id is the same, but the id is different. so when we first create the product we do two queries,
insert, then update table whatever set product_id = id where id = {the insert id}
This works, but I am wondering if there is a way to do it in one query?
Note we only have access to insert, update, delete. no triggers or stored procedures.
Use the LAST_INSERT_ID() function:
update table whatever set
product_id = id
where id = last_insert_id()
This is the single query:
insert into whatever
set product_id = last_insert_id() + 1;