I have two tables called TEMPDATA and Product. I am dumping all my data from a csv feed into TEMPDATA and then organising it into the respective tables, Product being one of them.
I am trying to insert all the contents of TEMPDATA that does not have a duplicate 'ean' number into Product, providing it doesn't already exist in Product.
The query I am trying to use is below...
INSERT IGNORE INTO `Product` (`product_ean`, `product_name`, `product_description`, `product_image`, `product_thumbnail`, `product_category`)
SELECT `ean`, `product_name`, `description`, `merchant_image_url`, `aw_thumb_url`, `merchant_category`
FROM (
SELECT `ean`, `product_name`, `description`, `merchant_image_url`, `aw_thumb_url`, `merchant_category`, count( * )
FROM TEMPDATA
GROUP BY `ean`
HAVING count( * ) = 1
) AS t
WHERE NOT EXISTS (
SELECT `product_ean` FROM Product
)
All the individual sections seem to be working, except from when I include the 'WHERE NOT EXISTS' clause, could someone please help me out?
try this:
INSERT IGNORE INTO Product (product_ean, product_name, product_description, product_image, product_thumbnail, product_category)
SELECT ean, product_name, description, merchant_image_url, aw_thumb_url, merchant_category
FROM (
SELECT ean, product_name, description, merchant_image_url, aw_thumb_url, merchant_category, count( * )
FROM TEMPDATA
GROUP BY ean
HAVING count( * ) = 1
) AS t
WHERE NOT EXISTS (
SELECT product_ean FROM Product AS A WHERE A.product_ean = t.product_name
)
you were missing this:
WHERE A.product_ean = t.product_name
in the NOT EXIST statement
you need to make you include the the where clause to search, see below
INSERT IGNORE INTO `Product` (`product_ean`, `product_name`, `product_description`, `product_image`, `product_thumbnail`, `product_category`)
SELECT `ean`, `product_name`, `description`, `merchant_image_url`, `aw_thumb_url`, `merchant_category`
FROM (
SELECT `ean`, `product_name`, `description`, `merchant_image_url`, `aw_thumb_url`, `merchant_category`, count( * )
FROM TEMPDATA
GROUP BY `ean`
HAVING count( * ) = 1
) AS t
WHERE NOT EXISTS (
SELECT `product_ean` FROM Product P where t.ean = p.`product_ean`
)
Related
insert ignore into user_login_history
(`created_at`,`updated_at`,`created_by`,
`updated_by`,`USER_ID`,`TENANT`,
`LAST_LOGIN`,`deleted`,`published`)
values(
(SELECT
a.created_at,
a.updated_at,
a.created_by,
a.updated_by,
user.id,
a.tenant,
a.created_at,
b'0',
b'1'
FROM
(SELECT audit_log_summary.*
FROM audit_log_summary, (SELECT
user, MAX(created_at) AS created_at, tenant
FROM
audit_log_summary
WHERE
audit_log_summary.REVISION_TYPE = 'LOGGED_IN'
GROUP BY user , TENANT) max_user
WHERE
audit_log_summary.user = max_user.user
AND audit_log_summary.tenant = max_user.tenant
AND audit_log_summary.created_at = max_user.created_at) a
INNER JOIN
user ON a.user = user.email));
error i am getting :
Error Code: 1136. Column count doesn't match value count at row 1
basically I have 3 tables i want to populate data in one table fetching the data from other two..
Table to be populated: user_login_history
Tables From which datas are fetched: audit_log_summary & user
You have to remove VALUES from the clause, so instead of using INSERT INTO VALUES, use INSERT INTO SELECT FROM. Maybe SELECT audit_log_summary.* you should select the needed columns only. In your select you are selecting a.created_at twice, check if it is what you want.
Maybe this will help:
insert ignore into user_login_history (`created_at`,
`updated_at`,
`created_by`,
`updated_by`,
`USER_ID`,
`TENANT`,
`LAST_LOGIN`,
`deleted`,
`published`)
(SELECT
a.created_at,
a.updated_at,
a.created_by,
a.updated_by,
user.id,
a.tenant,
a.created_at,
b'0',
b'1'
FROM
(SELECT audit_log_summary.*
FROM audit_log_summary, (SELECT
user, MAX(created_at) AS created_at, tenant
FROM
audit_log_summary
WHERE
audit_log_summary.REVISION_TYPE = 'LOGGED_IN'
GROUP BY user , TENANT) max_user
WHERE
audit_log_summary.user = max_user.user
AND audit_log_summary.tenant = max_user.tenant
AND audit_log_summary.created_at = max_user.created_at) a
INNER JOIN
user ON a.user = user.email));
Why in MYSQL by executing this SQL query 2 rows will add to table? Is this query executes two times!?;
INSERT INTO user(`usr_name`, `email`, `name`, `reg_date`, `role_id`)
(
SELECT "editor1",
"editor1#example.com",
"editor1",
"2005-12-20",
2
FROM `user`
WHERE (("admin", 3) IN (
SELECT usr_name, role_id
FROM `user`
)
AND NOT EXISTS (
SELECT usr_name, email
FROM `user`
WHERE usr_name = "editor1" OR email = "editor1#example.com"
))
)
result is here!
Apparently, two rows in user match the WHERE conditions.
You are not using the user table in the first FROM. So how about this instead:
INSERT INTO user(`usr_name`, `email`, `name`, `reg_date`, `role_id`)
SELECT t.*
FROM (SELECT 'editor1' as user_name, 'editor1#example.com as email,
'editor1' as name, '2005-12-20 as reg_date, 2 as role_id
) t
WHERE ('admin', 3) IN (SELECT usr_name, role_id
FROM `user`
) AND
NOT EXISTS (SELECT usr_name, email
FROM `user` u
WHERE u.usr_name = t.usr_name OR u.email = t.email
)
Or, better yet, but unique indexes on the fields that you don't want duplicated in the table:
create unique index idx_user_username on user(usr_name);
create unique index idx_user_email on usr(email);
Let the database protect the table. It is there to help you.
I am getting a #1064 error from this code I am trying to use a subquery within the values command I am new to mySQL and cannot spot the mistake.
CREATE TEMPORARY TABLE TempTable(myid INT,points INT)
INSERT INTO TempTable
values(1,
points
(
SELECT player.team, COUNT( * ) AS count
FROM playerpoints
JOIN player ON playerpoints.PlayerID = player.PlayerID
WHERE player.team = 'ManU'
AND EXTRACT( MONTH FROM playerpoints.date ) <07
GROUP BY player.team
));
INSERT INTO TempTable
values(2,
points
(
SELECT player.team, COUNT( * ) AS count2
FROM playerpoints
JOIN player ON playerpoints.PlayerID = player.PlayerID
WHERE player.team = 'ManU'
AND EXTRACT( MONTH FROM playerpoints.date ) >07
GROUP BY player.team
));
DROP TABLE TempTable;
The construct you want to use is INSERT INTO ... SELECT .... You don't use a subquery (or the VALUES keyword).
INSERT INTO TempTable (myid, points)
SELECT player.team, COUNT( * ) AS count
FROM playerpoints
JOIN player ON playerpoints.PlayerID = player.PlayerID
WHERE player.team = 'ManU'
AND EXTRACT( MONTH FROM playerpoints.date ) <07
GROUP BY player.team;
Basically, you want the SELECT to return the row(s) you're inserting (in the correct order).
Docs: http://dev.mysql.com/doc/refman/5.0/en/insert-select.html
No semicolon at the end of the first line. Also the INSERT syntax can be cleaned up a bit. Finally, the GROUP BY clause is not needed. Try this:
Try this:
CREATE TEMPORARY TABLE TempTable(myid INT,points INT);
INSERT INTO TempTable
values(1, (
SELECT COUNT( * )
FROM playerpoints
JOIN player ON playerpoints.PlayerID = player.PlayerID
WHERE player.team = 'ManU'
AND EXTRACT( MONTH FROM playerpoints.date ) <07
));
INSERT INTO TempTable
values(2, (
SELECT COUNT( * )
FROM playerpoints
JOIN player ON playerpoints.PlayerID = player.PlayerID
WHERE player.team = 'ManU'
AND EXTRACT( MONTH FROM playerpoints.date ) >07
));
...
DROP TABLE TempTable;
This assumes that playerpoints records one point per entry. Otherwise you'll need something like SELECT SUM(playerpoints.points) instead of SELECT COUNT(*).
I have a SQL command which I know data exists with the criteria, but when I run the query it doesn't return rows:
SQL:
SELECT `USER_ID`, `CAR_ID`, `AD_ID`, `BRAND`, `MODEL`
, `YEAR`, `PRICE`, `MILEAGE`, `GEARBOX`, `STATUS`, `COLOR_IN`, `COLOR_OUT`
, `BODY`, `FUEL`, `ABOUT`, `MAIN_PHOTO`
FROM (`ads`, `cars`, `ad_extras`)
WHERE `ads`.`USER_ID` = '2'
What should I do?
EDIT:
When I do SELECT * it also doesn't return data.
when I select from 2 tables it retaurns data. So I can't select from 3 tables right?
It's likeky that one of your tables is empty. The result of that is empty.You should use a query like this one
SELECT `USER_ID`, `CAR_ID`, `AD_ID`, `BRAND`, `MODEL`, `YEAR`, `PRICE`, `MILEAGE`, `GEARBOX`, `STATUS`, `COLOR_IN`, `COLOR_OUT`, `BODY`, `FUEL`, `ABOUT`, `MAIN_PHOTO` FROM `ads` natural left join `cars` natural left join `ad_extras` WHERE `ads`.`USER_ID` = '2'
I wanted to post this like a comment but I don't have enought privilegies.
In order to query from 3 table it is better to use JOIN clause between them
or if you want to write it as it is use alias for selecting columns
e.g
SELECT 'ads.USER_ID', 'cars.CAR_ID', 'ads.AD_ID'
FROM ('ads', 'cars', 'ad_extras')
WHERE 'ads'.'USER_ID' = '2'
with their respective table
Can anyone help me with optimizing this query. I can not execute it because phpMyAdmin connection gets timed out and i have no access to change time out parameter.
Here is a query:
INSERT INTO `goods` (`goods_id`, `price`, `name`)
SELECT `sales_goods`.`goods_id`, `sales_goods`.`price`, `sales_goods`.`goods_id`
FROM `sales_goods`
WHERE `sales_goods`.`sales_goods_id`
IN (
SELECT MAX(`sales_goods`.`sales_goods_id`)
FROM `sales_goods`
WHERE `sales_goods`.`sales_goods_id`
IN (
SELECT `sales_goods`.`sales_goods_id` FROM `sales_goods`
WHERE `sales_goods`.`goods_id` NOT IN (SELECT `goods`.`goods_id` FROM `goods`)
)
GROUP BY `sales_goods`.`goods_id`
)
Aside from ensuring your tables are propery indexed you can replace your IN subqueries with INNER JOIN, and your NOT IN subqueries WITH LEFT JOIN/IS NULL, this should improve the performance:
INSERT INTO `goods` (`goods_id`, `price`, `name`)
SELECT `sales_goods`.`goods_id`,
`sales_goods`.`price`,
`sales_goods`.`goods_id`
FROM `sales_goods`
INNER JOIN
( SELECT MAX(`sales_goods`.`sales_goods_id`)
FROM `sales_goods`
LEFT JOIN `goods`
ON `goods`.`goods_id` = `sales_goods`.`goods_id`
WHERE `goods`.`goods_id` IS NULL
GROUP BY `sales_goods`.`goods_id`
) MaxSalesGoods
ON MaxSalesGoods.sales_goods_id = `sales_goods`.`sales_goods_id`