Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple insert in multiple tables if nonindexed field not exists
    primarykey
    data
    text
    <p>I have a task to update existing multiple product-related tables with new entries if product name doesn't exist (no update). Thing is that this is one time action so product name column is not and will not be set as unique, primary, etc (and it is not possible for me to change it anyway).</p> <p>I would be delighted if i could access the database remotely and use PHP, but due to security reasons i have access only to PhpMyAdmin on that server. After googling for couple of hours i realized that either all results are about single INSERT IGNORE or similar constructions, or they are too complicated for me non-mysql brain to understand or solutions just won't work (like IF statement throws syntax error on that 5.5.8 MySQL server.</p> <p>So thing is - there are 7 tables with all kinds of information on product (product, variant, image, category, product relation to category, prices, discounts). I have to check if product name doesn't exist and if true then insert new entries in all 7 tables. Additionaly i should create new product category entry if such doesn't exist (again, by name, not id)</p> <p>In my humble opinion it would be like this:</p> <pre><code>IF (SELECT ProductName from `product` where ProductName='Pony') IS NULL THEN INSERT INTO `product` VALUES ('', 'Pony'); @productId = (SELECT LAST_INSERT_ID()); INSERT INTO `category` (Name) SELECT 'Everything Nice' FROM `category` WHERE NOT EXISTS (SELECT Name FROM `category` WHERE Name='Everything Nice') LIMIT 1; SET @categoryId = (SELECT CategoryId FROM `category` WHERE Name='Everything Nice'); INSERT INTO `product_rel_category` VALUES (@productId, @categoryId); INSERT INTO `variant` VALUES ('', 'Nice'); @variantId = (SELECT LAST_INSERT_ID()); INSERT INTO `product_rel_variant` VALUES (@productId, @variantId); INSERT INTO `variant` VALUES ('', 'Sweet'); @variantId = (SELECT LAST_INSERT_ID()); INSERT INTO `product_rel_variant` VALUES (@productId, @variantId); INSERT INTO `variant` VALUES ('', 'Pink'); @variantId = (SELECT LAST_INSERT_ID()); INSERT INTO `product_rel_variant` VALUES (@productId, @variantId); ... etc ... ENDIF; </code></pre> <p>MySQL yells about syntax on IF in such query. I also tried working all kinds of procedures, but since I know only 'insert, update, select, delete', there is nothing much i can compose now.</p> <p>So do you have an advise on solution on how i can make multiple INSERT/SELECT queries if a value in one table doesn't exist. </p> <p>Sincerely thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload