hello i am trying to search for duplicates in a table within a period using this snippet.
FROM
table WHERE Start_Date BETWEEN '2018-07-01' AND '2018-07-31'
GROUP BY Policy_Number
HAVING COUNT(Policy_Number) > 1;
this produces all the duplicates records in the table within the required dates with their counts.
Now i am trying to delete those duplicate records using this snippets using this snippet i have also found online
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id < t2.id AND t1.Policy_Number = t2.Policy_Number AND Start_Date BETWEEN '2018-07-01' AND '2018-07-31';
but i keep getting this error
Column 'Start_Date' in where clause is ambiguous
Please how can i correct this to delete the duplicates i want removed thanks!!
write like this way t1.Start_Date its work
Try running as a query previous executing your select:
SET SQL_BIG_SELECTS=1;
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id < t2.id AND t1.Policy_Number = t2.Policy_Number AND t1.Start_Date BETWEEN '2018-07-01' AND '2018-07-31';
Because you create t1 and t2 using same table table so both have start_date and thats why its give Column 'Start_Date' in where clause is ambiguous error
Define the alias before start_Date
SET OPTION SQL_BIG_SELECTS = 1
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id < t2.id AND t1.Policy_Number = t2.Policy_Number AND t1.Start_Date BETWEEN '2018-07-01' AND '2018-07-31'
an example works
SELECT
table.Number
FROM table
INNER JOIN table2
ON table.ID = table2.ID
WHERE checkInDate BETWEEN '2015-09-12' AND '2015-09-13';
Related
I have two table:
Can't run query, to display "Result" table, pls help.
Consider:
SELECT
t1.id,
t2.id1,
t2.col
FROM table1 t1
INNER JOIN table2 t2
ON t1.nr1 BETWEEN t2.MinNr1 AND t2.MaxNr1
AND t1.nr2 BETWEEN t2.MinNr2 AND t2.MaxNr2
Work only with:
INNER JOIN table2 t2
On t1.nr1 >= t2.MinNr1 And t1.<= t2.MaxNr1
And t1.nr2 >= t2.MinNr2 And t2.MinNr2 <= t2.MaxNr2
delete t1 from pg_acymailing_listsub t1
join pg_acymailing_subscriber tt on t1.subid = tt.subid
join pg_users t2 on tt.userid = t2.id
where NOW() > t2.registerDate + interval 90 day and t1.listid=7 and (
exists (
SELECT * FROM pg_acymailing_listsub WHERE pg_acymailing_listsub.listid=1 AND pg_acymailing_listsub.subid = t1.subid AND pg_acymailing_listsub.status=-1 LIMIT 1
) OR t1.subid not in (
select pg_acymailing_userstats.subid from pg_acymailing_userstats where pg_acymailing_userstats.subid = t1.subid and pg_acymailing_userstats.open > 0
)
);
What the problem? I think there need to use subqueries, but I not imagine how to use it in my query.
I admit that the error message is hard to follow. It should really say delete. But the problem is the reference to pg_acymailing_listsub in the EXISTS subquery, and that is among the tables you want to delete from.
MySQL does not allow references in subqueries to the table being deleted from . . . unless you use a materialization hack.
However, this should be easy to fix by adding another JOIN clause to the FROM. You seem to know how to do that.
Try this:
delete t1 from pg_acymailing_listsub t1
join pg_acymailing_subscriber tt on t1.subid = tt.subid
join pg_users t2 on tt.userid = t2.id
join pg_acymailing_listsub t3 on t3subid=t1.subid and t3.listid=1 and t3.[status]=-1
join pg_acymailing_userstats t4 on t4.subid = t1.subid AND t4.[open]<=0
where NOW() > t2.registerDate + interval 90 day and t1.listid=7
I want to get the most recent row from each inner joined tables. Both tables have a timestamp field. Below is what I have so far. But it only targets for table1 how about for table2?
SELECT
`table1`.`fieldX`,
`table2`.`fieldY`
FROM `db`.`table1`
INNER JOIN `db`.`table2`
ON `table1`.`id` = `table2`.`id`
WHERE `table1`.`id` = ?
ORDER BY `table1`.`timestamp`
DESC LIMIT 1
table1
row_id
id
fieldX
timestamp
table2
row_id
id
fieldY
timestamp
Both tables can have repeating ids. It was designed this way to store older versions of the data entries.
For example: table1 can have 3 rows with the same id while table2 can have 2 rows of the same id. I want to get the latest row from both tables.
Use can use it like this if you want recent record from table2
SELECT SUBSTRING_INDEX(GROUP_CONCAT(t1.fieldX ORDER BY t1.timestamp DESC),',',1) as field_x, SUBSTRING_INDEX(GROUP_CONCAT(t2.fieldY ORDER BY t2.timestamp DESC),',',1) as field_y
FROM table1 t1
JOIN table2 t2 ON(t2.id = t1.id)
GROUP BY t1.id
ORDER BY t1.timestamp
I have a query now which works great for finding player info including their rank:
SELECT t1.pid,
t1.type,
t1.pspeed,
t1.distance,
t1.maxspeed,
t1.prestrafe,
t1.strafes,
t1.sync,
t1.wpn,
1+SUM(t2.pid IS NOT NULL) as rank
FROM records t1
LEFT JOIN records t2
ON t1.type = t2.type
AND t1.pspeed = t2.pspeed
AND t1.distance < t2.distance
WHERE t1.pid = "'.$pid.'"
AND t1.type IN ("type1","type2","type3")
GROUP BY t1.type, t1.pspeed
The problem now is that I wish to use an authid instead of a pid as my variable. I can do an easy join on the records table along with a player table that has an id equal to the pid in the records table as well as the authid I wish to search by:
SELECT * FROM records JOIN players ON records.pid=players.id
I wish to use this second query in place of the FROM records portion of the first query but I've confused myself in my attempts via the many aliases used.
Here's a pitiful example of an attempt:
SELECT t1.pid,
t1.type,
t1.pspeed,
t1.distance,
t1.maxspeed,
t1.prestrafe,
t1.strafes,
t1.sync,
t1.wpn,
1+SUM(t2.pid IS NOT NULL) as rank
FROM (
SELECT *
FROM records
JOIN players ON records.pid=players.id
) t3 t1
LEFT JOIN records t2
ON t1.type = t2.type
AND t1.pspeed = t2.pspeed
AND t1.distance < t2.distance
WHERE t1.authid = "'.$authid.'"
AND t1.type IN ("type1","type2","type3")
GROUP BY t1.type, t1.pspeed
Anyone able to whip me into shape please feel free to do so. I'm awful at SQL still.
Your Join looks pretty close. However you gave your subquery two aliases. Try just one
SELECT t1.pid, t1.type, t1.pspeed, t1.distance, t1.maxspeed, t1.prestrafe, t1.strafes, t1.sync, t1.wpn, 1+SUM(t2.pid IS NOT NULL) as rank
FROM
(
SELECT players.*, rec.pid, rec.type, rec.pspeed, rec.distance, rec.maxspeed,
rec.prestrafe, rec.strafes, rec.sync, rec.wpn
FROM records AS rec JOIN players ON rec.pid = players.id ) AS t1
LEFT OUTER JOIN records t2
ON t1.type = t2.type AND t1.pspeed = t2.pspeed AND t1.distance < t2.distance
WHERE t1.authid = "'.$authid.'" AND
t1.type IN ("type1","type2","type3")
GROUP BY t1.type, t1.pspeed
Also, naming your columns in your subquery should eliminate the duplicate column error.
I have two different tables
in first one (t1) I have
Table [t1]
id Product_URL
.
Table [t2]
id Product_id Product_URL
I would like to UPDATE ALL product_id field (from t2) to the id of the first
WHERE t1.product_url = t2.product_url
can I do that In one query?
UPDATE t2
JOIN (
SELECT t2_2.id, t1.id as new_id
FROM
t2 t2_2 JOIN
t1 ON t2_2.product_url = t1.product_url AND t2_2.product_id <> t1.id
ORDER BY t2_2.id
LIMIT 5000
) sub ON t2.id = sub.id
SET id = sub.new_id;
EDIT: It looks like mutli-table updates do not play well with LIMIT and ORDER BY, but here's another query that accomplishes the same thing...