How to delete already Select table? - mysql

I have selected Duplicate table using Following Query
SELECT
y.Entity,y.ExpenseTypeCode,y.ExpenseType,y.LOB,y.Center,y.Location,y.Amount,y.APSupplierID,y.ExpenseReportID,y.Employee,y.ReportDate,y.ExpenseDate,y.Description
FROM dbo.TotalsByGLCenter$ y
INNER JOIN (SELECT
Entity,ExpenseTypeCode,ExpenseType,LOB,Center,Location,Amount,Description,APSupplierID,ExpenseReportID,Employee,ReportDate,ExpenseDate, COUNT(*) AS CountOf
FROM dbo.TotalsByGLCenter$
GROUP BY Entity,ExpenseTypeCode,ExpenseType,LOB,Center,Location,Amount,APSupplierID,ExpenseReportID,Employee,ReportDate,ExpenseDate,Description
HAVING COUNT(*)>1
) dt ON y.Entity=dt.Entity and y.ExpenseTypeCode=dt.ExpenseTypeCode and y.ExpenseType=dt.ExpenseType and y.LOB=dt.LOB and y.Location=dt.Location and y.Center=dt.Center and y.Amount=dt.Amount and y.APSupplierID=dt.APSupplierID and y.ExpenseReportID=dt.ExpenseReportID and y.Employee=dt.Employee and y.ReportDate=dt.ReportDate and y.ExpenseDate=dt.ExpenseDate and y.Description=dt.Description
i Want Delete selected Query . how can I use delete Query Above Query Statement ?

Try this mate ,
DELETE FROM your_table WHERE id IN (
Select (
your select query
) As alias
)

Related

Delete records based on another query in mysql

I have a query in MySQL based on which I am finding duplicate records of some columns.
select max(id), count(*) as cnt
from table group by start_id, end_id, mysqltable
having cnt>1;
This above query gives me the max(id) and the count of number of records that have start_id,end_id,mysqltable column values same.
I want to delete all the records that match the max(id) column of the above query
How can I do that?
I have tried like below
delete from table
where (select max(id), count(*) as cnt
from table group by start_id,end_id,mysqltable
having cnt>1)
But Unable to delete records
You can remove duplicate records using JOIN.
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id > t2.id AND t1.start_id = t2.start_id AND t1.end_id = t2.end_id AND t1.mysqltable = t2.mysqltable;
This query keeps the lowest id and remove the highest.
I think so this command should work:
delete from table
where id in
( select max(id) from table
group by start_id, end_id, mysqltable
having count(*) > 1
);

insert into table(var1) select from itself

I want to insert into the last column (number of people in that room) and
I want to use
insert into table(n_people_in_room)
select count(people_id)
from table
group by room
This is obvious wrong because i need to join the table with itself on people_id but i didn't. What is the right code?
Here's one way to do it, using an inline view to get the N_People_In_Room totals:
I'd do it as a SELECT first:
SELECT t.peopleid
, t.room
, t.n_people_in_room AS `old_npir`
, s.n_people_in_room AS `new_npir`
FROM mytable t
JOIN ( SELECT c.room
, COUNT(1) AS n_people_in_room
FROM mytable c
GROUP BY c.room
) s
ON s.room = t.room
Convert that into an UPDATE by repacing SELECT ... FROM with UPDATE, and adding a SET clause...
UPDATE mytable t
JOIN ( SELECT c.room
, COUNT(1) AS n_people_in_room
FROM mytable c
GROUP BY c.room
) s
ON s.room = t.room
SET t.n_people_in_room = s.n_people_in_room

while updating same table in mysql skipping some row

I am trying to update same table by join of same table, duplicate row skipping.I want to store duplicate value's count in number_of_duplicates field.
UPDATE 4_crem_three_entries a,(
SELECT count(*) c,cream_id FROM 4_crem_three_entries
GROUP BY `c_phone` HAVING count( * ) >1
) b
SET number_of_duplicates=b.c
WHERE a.cream_id=b.cream_id
Try this
UPDATE 4_crem_three_entries
SET number_of_duplicates=(SELECT count( * ) FROM 4_crem_three_entries c where c.cream_id=4_crem_three_entries.cream_id GROUP BY c.c_phone HAVING count( * ) >1)

using the same field within a subquery of mysql

how can i make something like this work?
INSERT INTO age.page(domain,title_count,youtube_count,ipaddress,updated)
SELECT * FROM
(
SELECT domain,
COUNT(domain) AS titlecount,
(SELECT COUNT(*) FROM table2 WHERE title = table1.title) AS YoutubeCount, ipaddress
NOW() AS timeNow
FROM table1
GROUP BY domain
ORDER BY title DESC
) a;
I want to use a subquery to get a count of a different table but use the same field from the main query.
the reason i want to do this is so i dont have to run two queries instead its only one.
You can do this COUNT in a subquery and then JOIN it with the first table:
INSERT INTO age.page(domain, title_count, youtube_count, ipaddress, updated)
SELECT * FROM
(
SELECT
domain,
COUNT(domain) AS titlecount,
t2.titlecount AS YoutubeCount,
ipaddress,
NOW() AS timeNow
FROM table1
INNER JOIN
(
SELECT title, COUNT(*) Titlecount
FROM table2
GROUP BY title
) AS t2 ON t2.title = table1.title
GROUP BY domain
ORDER BY table1.title DESC
) a;

Confused with UPDATE query. How to convert select to update with MySQL?

I have seen very similar if not same questions on here but my trials of trying to convert following query into an UPDATE statement failed.
SELECT table.* FROM table JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column) WHERE t.rank = 1
ORDER BY t.rank DESC
I want to update column of all results selected using the query above.
How can I convert this into an update statement?
Thank you.
This should do it:
update table
set column = 'somevalue'
where id in
(select id from (
SELECT table.* FROM table JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column) WHERE t.rank = 1) x)
not entirely sure but i think it's something like
update tblname set columname = value where tblname.columncompare = (select statement)
INSERT INTO table (id, value)
SELECT table.id, table.value
FROM table
JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column)
WHERE t.rank = 1
ORDER BY t.rank DESC
ON DUPLICATE KEY UPDATE value = VALUES(value)
Insert on duplicate to the rescue!
Basicly this allows you to do any SELECT as normal and then you prepend INSERT INTO and append ON DUPLICATE.
I guess that this query is made up, but what's the point of filtering and ordering the same column?