-1

The problem is where name_1 writes something to name_2 and name_2 writes something to name_1 there is a duplicate and as one result because I SELECT name_1, name_2 with condition

SELECT name_1, name_2 
FROM class_room 
WHERE (name_1='$userInSystem' AND name_2 <>'$userInSystem') 
    OR (name_1<>'$userInSystem' AND name_2 ='$userInSystem' ) 
GROUP BY name_1, name_2 
ORDER BY id DESC

How is it possible to show results with no duplicate rows,

I only want to know if there is communication between user_1 and user_2

Dharman
  • 30,962
  • 25
  • 85
  • 135
shon
  • 13
  • 4
  • just write distinct after the select? Could you please provide some sample data and the desired result? – Jonas Metzler Apr 26 '22 at 17:11
  • I tried, distinct does not work – shon Apr 26 '22 at 17:13
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Apr 26 '22 at 17:13

1 Answers1

0

Order the names in a consistent way, then make it DISTINCT.

SELECT DISTINCT LEAST(name_1, name_2) AS name1, GREATEST(name_1, name_2) AS name2
FROM class_room
WHERE (name_1='$userInSystem' AND name_2 <>'$userInSystem') 
    OR (name_1<>'$userInSystem' AND name_2 ='$userInSystem' ) 
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I was looking for a way to integrate user_name1 AND user_name2 AS one and found no way, And what you suggested worked, so thank you Thanks :))) have a good day – shon Apr 26 '22 at 17:21