MySQL - How to remove duplicate data entry? - mysql

I have below kind of data
id product_id url
10 300 /300/abc
9 300 /300/abc/def
8 200 /200/bcd
7 200 /200/bcd/efg
6 100 /100/cde
5 100 /100/cde/hif
Each rows product_id second value is correct means, id 9,7,5 are correct.
SELECT product_id, COUNT(*) count FROM demo_table GROUP BY product_id HAVING count > 1;
Above query will give me the result which have duplicate values.
How can I delete rest of the records via MySQL query?

Use a self join in the DELETE statement:
DELETE d1
FROM demo_table d1 INNER JOIN demo_table d2
ON d2.product_id = d1.product_id AND d2.id < d1.id;
See the demo.
Results:
| id | product_id | url |
| --- | ---------- | ------------ |
| 9 | 300 | /300/abc/def |
| 7 | 200 | /200/bcd/efg |
| 5 | 100 | /100/cde/hif |

Related

How to select rows from two tables using MySQL?

I want to select information from two SQL tables within one query.
table1
id | postID | status | number | userID | active
1 | 1 | 100 | 100 | 3 | 1
2 | 7 | 50 | 25 | 5 | 1
3 | 3 | 75 | 50 | 3 | 1
table2
postID | reference | joint_date | userID | remove
1 | 100 | 100 | 3 | 1
2 | 50 | 25 | 5 | 1
3 | 50 | 50 | 3 | 0
Expected Output
postID | status | number | reference | joint_date
1 | 100 | 100 | 100 | 100
3 | 75 | 50 | 50 | 50
This is my try:
$userID = 12;
$active = 1;
$remove= 1;
$sql = "SELECT table1.status, table1.number, table2.reference, table2.joint_date
FROM table1
WHERE table1.active=:active AND table1.userID=:userID
INNER JOIN table2 ON table1.postID=table2.postID
WHERE table2.postID=:userID
AND table2.remove=:remove
ORDER BY table1.postID DESC";
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(":active", $active, PDO::PARAM_INT);
$stmt->bindValue(":userID", $userID, PDO::PARAM_INT);
$stmt->bindValue(":remove", $remove, PDO::PARAM_INT);
$stmt->execute();
What is wrong with my query?
What is wrong with my query?
The clauses/statements order are wrong
SELECT....FROM....WHERE....AND....AND....ORDER BY..
You can not have two WHERE condition in the same SELECT (do not take in consideration subqueries)
There is no way you can get the expected result based on the given static variables
Based on the data example you have two condition which will never be true at the same time.
AND t2.postID=3 --- > t2.remove is equal to 0 then you specify
AND t2.remove=1
You can use below example to properly filter based on your conditions:
SELECT t1.postID,
t1.status,
t1.`number`,
t2.reference,
t2.joint_date
FROM table1 t1
INNER JOIN table2 t2 ON t1.postID=t2.postID
WHERE t1.active=1
AND t1.userID=3
ORDER BY t1.postID ASC;
Result:
postID status number reference joint_date
1 100 100 100 100
3 75 50 50 50
Note. I removed both conditions table2.postID=:userID AND table2.remove=:remove
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=78b0f07c7d1dd1db1008e415fc005e85

How to find to "opposite" records on MySQL?

I have 1 table with the following cols:
giver_id | receiver_id
10 | 12
9 | 10
10 | 20
12 | 10
I am looking for a mysql query that will return 10-12 / 12-10 as a match.
Thanks
To identify the records for which an "opposite" record exist, you could do:
SELECT *
FROM mytable t
WHERE EXISTS (
SELECT 1
FROM mytable t1
WHERE t1.giver_id = t.receiver_id AND t.giver_id = t1.receiver_id
)
This demo on DB Fiddle with your sample data returns:
| giver_id | receiver_id |
| -------- | ----------- |
| 10 | 12 |
| 12 | 10 |

SQL Query to CAST and SUM columns in two different Tables having Multiple Entries

Need help in Building a Query which can select a Item and sum up its QTY which is in 2 different Tables.
Example Scenario:
Table 1:
ID | ITEM_NAME | QTY |
1 | Item_Desc1 | 0 |
2 | Item_Desc2 | 2 |
3 | Item_Desc3 | 4 |
4 | Item_Desc4 | 0 |
Table 2:
ID | ITEM_ID | BATCHNO | QTY
1 | 1 | B1 | 100
2 | 1 | B2 | 100
3 | 2 | B3 | 0
4 | 3 | B2 | 100
5 | 4 | B2 | 200
6 | 4 | B3 | 100
Need a Query to show in a Data grid Table as follows:
ID | Item_Name | QTY
1 | Item_Desc1 | 200
2 | Item_Desc2 | 2
3 | Item_Desc3 | 104
4 | Item_Desc4 | 300
Note:
This Query should be useful to run a Search query using 'LIKE' operator by using which User can search for Items Needed
SAMPLE Image of My APPLICATION to Run Search query using Table 1 and Table 2
SAMPLE Image of My APPLICATION DATABASE - TABLE2
SAMPLE Image of My APPLICATION DATABASE - TABLE1
To get sum from 2 table you can use following query
select a.ID, a.ITEM_NAME, a.QTY + coalesce(b.QTY,0)
from Table1 a
left join (
select ITEM_ID,sum(QTY) QTY
from Table2
group by ITEM_ID
) b on a.ID = b.ITEM_ID
Demo
Try the following query
select tbl1.id as ID,tbl1.Item_Name, tbl.qty+sum(tbl2.qty) as QTY
from table1 as tbl1 JOIN table2 as tbl2
ON tbl1.id = tbl2.id group by tbl2.item_id

How to delete unknown number last record (on condition)?

Could you please tell me how to delete unknown number last record (on condition)?
For example, in this situation I want to delete record with id: 6 to 10.
Note: this table and records is not constant.
+----+-----+---------+
| id | url | emailid |
+----+-----+---------+
| 1 | 10 | 1 |
| 2 | 20 | 0 |
| 3 | 30 | 2 |
| 4 | 40 | 0 |
| 5 | 50 | 10 |
| 6 | 60 | 0 |
| 7 | 70 | 0 |
| 8 | 80 | 0 |
| 9 | 90 | 0 |
| 10 | 100 | 0 |
+----+-----+---------+
Thanks...
It seems that you want to delete the last set of records where all the values are 0. This is a bit of a pain. You can find the minimum such id as:
select min(t.id)
from table t
where t.emailid = 0 and
not exists (select 1 from table t2 where t2.id > t.id and t2.emailid <> 0);
The logic is: find all rows where emailid is 0 and there are no subsequent emailids that are not zero.
You can put this into a delete using join:
delete t
from table t cross join
(select min(t.id) as first0id
from table t
where t.emailid = 0 and
not exists (select 1 from table t2 where t2.id > t.id and t2.emailid <> 0)
) tmin
where t.id >= tmin.first0id;
You can use between keyword in your query like this
delete from yourtable where id BETWEEN 6 AND 10
use this query
delete from your_table where id between 6 AND 10
for not being constant you can first store start and end values in variable and then pass in query,example(in php)
$start = 6 ;
$end = 10;
query
"delete from your_table where id between $start AND $end"

MySQL - Get row with the maximum HISTORY ID for COMPONENT IDs in non-existing months

I have a table INVENTORY which consists of inventory items. I have the following table structure:
INSTALLATION_ID
COMPONENT_ID
HISTORY_ID
ON_STOCK
LAST_CHANGE
I need to obtain the row with the max HISTORY ID for records for which the spcified LAST_CHANGE month doesn't exist.
Each COMPONENT_ID and INSTALLATION_ID can occur multiple times, they are distinguished by their respective HISTORY_ID
Example:
I have the following records
COMPONENT_ID | INSTALLATION_ID | HISTORY_ID | LAST_CHANGE
1 | 100 | 1 | 2013-01-02
1 | 100 | 2 | 2013-02-01
1 | 100 | 3 | 2013-04-09
2 | 100 | 1 | 2013-02-22
2 | 100 | 2 | 2013-03-12
2 | 100 | 3 | 2013-07-07
2 | 100 | 4 | 2013-08-11
2 | 100 | 5 | 2013-09-15
2 | 100 | 6 | 2013-09-29
3 | 100 | 1 | 2013-02-14
3 | 100 | 2 | 2013-09-23
4 | 100 | 1 | 2013-04-17
I am now trying to retrieve the rows with the max HISTORY ID for each component but ONLY for COMPONENT_IDs in which the specifiec month does not exists
I have tried the following:
SELECT
INVENTORY.COMPONENT_ID,
INVENTORY.HISTORY_ID
FROM INVENTORY
WHERE INVENTORY.HISTORY_ID = (SELECT
MAX(t2.HISTORY_ID)
FROM INVENTORY t2
WHERE NOT EXISTS
(
SELECT *
FROM INVENTORY t3
WHERE MONTH(t3.LAST_CHANGE) = 9
AND YEAR(t3.LAST_CHANGE)= 2013
AND t3.HISTORY_ID = t2.HISTORY_ID
)
)
AND INVENTORY.INSTALLATION_ID = 200
AND YEAR(INVENTORY.LAST_CHANGE) = 2013
The query seems to have correct syntax but it times out.
In this particular case, i would like to retrieve the maximum HISTORY_ID for all components except for those that have records in September.
Because I need to completely exclude rows by their month, i cannot use NOT IN, since they will just suppress the records for september but the same component could show up with another month.
Could anybody give some pointers? Thanks a lot.
If I understand correctly what you want you can do it like this
SELECT component_id, MAX(history_id) history_id
FROM inventory
WHERE last_change BETWEEN '2013-01-01' AND '2013-12-31'
AND installation_id = 100
GROUP BY component_id
HAVING MAX(MONTH(last_change) = 9) = 0
Output:
| COMPONENT_ID | HISTORY_ID |
|--------------|------------|
| 1 | 3 |
| 4 | 1 |
If you always filter by installation_id and a year of last_change make sure that you have a compound index on (installation_id, last_change)
ALTER TABLE inventory ADD INDEX (installation_id, last_change);
Here is SQLFiddle demo