KMS
KMS

Reputation: 574

PHP how to find the duplicates

I am using php and I want to display list of records from database. Say for example, the following is the table structure

sno val1 val2 val3  amt
1    2c   3e   I    500
2    2b   i7   I    1500
3    2w   u9   I    0
4    18   e3   I    50
5    2c   3e   S    70

Now I want to display records like below,

sno val1  val2  value
1    2c    3e    1
2    2b    i7    1
3    2w    u9    1
4    18    e3    1
5    2c    3e    0 //because already listed

I'm using normal while loop to show this structure, but I don't know weather the value is already listed or not..

Can anyone help

Upvotes: 0

Views: 57

Answers (2)

G.Baghashvili
G.Baghashvili

Reputation: 220

You can try:

SELECT a.sno, a.val1, a.val2,
       CASE WHEN COUNT(*) > 1 THEN 0 ELSE 1 END value
FROM table_name a
JOIN table_name b 
ON a.val1 = b.val1 
   AND a.val2 = b.val2 
   AND a.sno >= b.sno
GROUP BY a.sno

Upvotes: 0

Stipic
Stipic

Reputation: 41

One solution:

$listed = array();
while() // your loop
{
    if(in_array($uniq_id_of_your_item, $listed)) {
         continue;
    }
    $listed[] = $uniq_id_of_your_item;

    // rest of your code
}

Second solution (better): you can limit that in SQL query

Upvotes: 2

Related Questions