This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 9 years ago.
I have a table and I need the correct statement to delete duplicate records. I want to achieve this by deleting rows in the table where multiple column values equal those of other records in the same table. I can do simple delete from [table] where [col] = [val] but I have no idea how to do this. Can you provide a template that I can work from?
Thanks.
delete from [table]
where [col] not in
(
select * from
(
select min([col])
from [table]
group by [col]
having count(distinct [val]) <> count([val])
) x
)
If you select from a table you are deleting from in MySQL then you have to use a subquery to hide that. That is why I used the select * from (...) x
Assuming that the original key's id is say 7,
DELETE FROM [table] WHERE [col1] = [val1] AND [col2] = [val2] .... AND [id] != 7;
Related
This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 1 year ago.
I am new to SQL and using MYSQL Workbench.
In this query, I am trying to find duplicate values and then delete the duplicates(anything > 1)
But I am getting the error "The target table RowNumCTE of the DELETE is not updatable"
-- Remove duplicates
With RowNumCTE as(
select *,
row_number() over (
partition by ParcelID,
PropertyAddress,
SalePrice,
SaleDate,
LegalReference
order by UniqueID) row_num
from portfolioproject.nashvillehousing)
delete
from RowNumCTE
where row_num > 1;
Your guidance will be highly appreciated.
You can't delete from a CTE on MySQL 8+, as you might have been able to do on SQL Server. But, we can rephrase your logic as follows:
DELETE
FROM portfolioproject.nashvillehousing p1
WHERE EXISTS (SELECT 1 FROM portfolioproject.nashvillehousing p2
WHERE p2.ParcelID = p1.ParcelID AND
p2.PropertyAddress = p1.PropertyAddress AND
p2.SalePrice = p1.SalePrice AND
p2.SaleDate = p1.SaleDate AND
p2.LegalReference = p1.LegalReference AND
p2.UniqueID < p1.UniqueID);
This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 2 years ago.
I went through the previous answer like this, but it gives me the following error : You can't specify target table 'table_name' for update in FROM clause.
I have a table with say 3 columns (id -> auto increment primary id) :
id, roll_no and attendance
And for selected roll numbers having many entries each, except the first entry I want to update all entry attendance field as P.
The query which I wrote is following :
UPDATE tbl_class_attendance
set attendance = 'P'
where id NOT IN
(Select min(id)
from tbl_class_attendance
WHERE roll_no IN ('25', '45', '55')
GROUP
BY roll_no;
But it gives me the above error.
I also went through other answers asking to use two select queries but there the answer I didn't find completely easy to understand as well as difficulty in executing for my selected list of roll numbers.
So, is there a way to update?
EDIT : Answer given below
UPDATE tbl_class_attendance t1
LEFT JOIN ( SELECT t2.roll_no, MIN(t2.id) id
FROM tbl_class_attendance t2
WHERE t2.roll_no IN ('25', '45', '55')
GROUP BY t2.roll_no ) t3 USING (roll_no, id)
SET t1.attendance = 'P'
WHERE t3.id IS NULL;
Got it working by following :
Update table SET a=value WHERE x IN
(Select * from (select x from table where condition) as t)
Credit : https://stackoverflow.com/a/43610081/6366458
This question already has answers here:
How to return rows that have the same column values in MySql
(3 answers)
Closed 6 years ago.
I am trying to find the rows that exist inside an index, however, my results keep coming up null even though there are rows that match my index. Is there something wrong with the logic of my query?
Here is my test query:
SELECT `the_products` . *
FROM `the_products`
INNER JOIN `producttypes_index` ON `producttypes_index`.`the_product_id` = `the_products`.`id`
AND `producttypes_index`.`type_id` = '1'
INNER JOIN `producthashtags_index` ON `producthashtags_index`.`the_product_id` = `the_products`.`id`
WHERE
`producthashtags_index`.`producthashtag_id`
IN ('41')
AND
`producthashtags_index`.`producthashtag_id`
IN ('42')
AND
`producthashtags_index`.`producthashtag_id`
IN ('6')
ORDER BY updated_at DESC
Here you can see the_product_id 54433 exists inside the index table producthashtags_index using query:
SELECT *
FROM `producthashtags_index`
WHERE `the_product_id` =54433
Results:
id producthashtag_id the_product_id
25433 6 54433
25434 41 54433
25435 42 54433
Then you can see it also exists inside the index table producttypes_index using query:
SELECT *
FROM `producttypes_index`
WHERE `the_product_id` =54433
Results:
type_id the_product_id
1 54433
Remove
AND `producttypes_index`.`type_id` = '1'
you can also optimaze your code by making the 3 IN`s into one like this:
AND `producthashtags_index`.`producthashtag_id` IN('42','41','6')
This question already has answers here:
Search text in fields in every table of a MySQL database
(27 answers)
Closed 9 years ago.
I have many tables:
d_customers
d_customers
d_news
d_pages
d_products
d_projects
d_sms
and I want to create a search form to search for any word typed in it in all columns of all tables... But when write SQL code I see that it's long and confusing... Can any one tell me the right way to do this?
'SELECT * FROM d_customers,d_customers,d_news,d_pages,d_products,d_projects,d_sms
WHERE ' . $nc_make . 'LIKE .....
AND LIKE.... AND LIKE..... AND LIKE..... AND LIKE..... '
i want to search into all coulmns in all tables by LIKE word...if i search for google word i want to select all coumns in all tables where all columns like google
create table t1(a int);
create table t2(a int, b int);
insert into t1 values (1);
insert into t2 values (1,3);
SELECT *
FROM (
(select 't1' as tbl, a as Col1, null as Col2 from t1)
union
(select 't2' as tbl, a as Col1, b as Col2 from t2)
) as U
where U.Col1 = 1 or U.Col2 = 1
Result:
TBL COL1 COL2
t1 1 (null)
t2 1 3
If the tables are related to each other then join them and them apply your condition.
select *
from customers c
inner join pages p
on p.customer_id=c.customer_id
where customer_name like 'xyz'
You cannot avoid joins and conditions in the sql if they are necessary but you can optimize them.
If you want to generate query dynamically using programming and then want to execute then in mysql information_schema stores all the information related to the tables and the fields contained in that table. You can use it to generate your dynamic sql.
Hope this helps.
This question already has answers here:
Get Identity after selecting from distinct result
(3 answers)
Closed 10 years ago.
i have 2 tables ( Model_Table , Items_Table)
Model_Tabl ( ID, ModelName, ModelQuantity)
Items_Tabl ( I_Code, IName, ID)
after inserting new row into (Model_Table) - Triggers insert multi row into (Items_Table) Depend on ModelQuantity from (Model_Table) , and until now its work fine
I Created
"select distinct ModelName , Sum(ModelQuantity) group by ModelName"
and i got result fine
My question is :
When i select model name from (DISTINCT) query i want to know which (ID) I selected from (Model_Table)
(Model_ID) to (ModelName) = 1 to many
ty
just select also the id ,try this
select id, ModelName , Sum(ModelQuantity)
from Model_Tabl group by ModelName
you dont need DISTINCT because you already making group by and they do same work