How to select/count rows where the same value exists in a column but not in other column all at the same table I have this table structure and data:

CREATE TABLE IF NOT EXISTS `default_relations_users` (
  `id_user_rq` int(11) NOT NULL,
  `id_user_ap` int(11) NOT NULL,
  UNIQUE KEY `rusers_rq_ap_idx` (`id_user_rq`,`id_user_ap`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `default_relations_users` (`id_user_rq`, `id_user_ap`) VALUES
(1, 2),
(1, 3),
(1, 4),
(2, 1),
(2, 2),
(2, 3);


I need to get only the rows where the value is on the left but not on the right, meaning for example: 1,2 and 2,1 will not be selected but 1,3 and 1,4 yes. I try this query:

SELECT * FROM default_relations_users
WHERE default_relations_users.id_user_rq = 1 
 AND NOT EXISTS (SELECT id_user_ap FROM default_relations_users WHERE id_user_ap = 1) 


Maybe I don't understand what Subqueries mean or I'm using in the wrong way. So any help?
SELECT A.*
FROM default_relations_users A
LEFT JOIN default_relations_users B
ON  A.id_user_rq = B.id_user_ap
AND A.id_user_ap = B.id_user_rq
WHERE B.id_user_rq IS NULL;


I loaded you sample data (I added an additional index)

mysql&gt; use Reynierpm
Database changed
mysql&gt; DROP TABLE IF EXISTS `default_relations_users`;
Query OK, 0 rows affected (0.00 sec)

mysql&gt; CREATE TABLE IF NOT EXISTS `default_relations_users` (
    -&gt;   `id_user_rq` int(11) NOT NULL,
    -&gt;   `id_user_ap` int(11) NOT NULL,
    -&gt;   UNIQUE KEY `rusers_rq_ap_idx` (`id_user_rq`,`id_user_ap`),
    -&gt;   UNIQUE KEY `rusers_ap_eq_idx` (`id_user_ap`,`id_user_rq`)
    -&gt; ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Query OK, 0 rows affected (0.05 sec)

mysql&gt; INSERT INTO `default_relations_users` (`id_user_rq`, `id_user_ap`) VALUES
    -&gt; (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3);
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql&gt; select * from default_relations_users;
+------------+------------+
| id_user_rq | id_user_ap |
+------------+------------+
|          1 |          2 |
|          1 |          3 |
|          1 |          4 |
|          2 |          1 |
|          2 |          2 |
|          2 |          3 |
+------------+------------+
6 rows in set (0.00 sec)

mysql&gt;


Here is the result of my answer

mysql&gt; SELECT A.*
    -&gt; FROM default_relations_users A
    -&gt; LEFT JOIN default_relations_users B
    -&gt; ON  A.id_user_rq = B.id_user_ap
    -&gt; AND A.id_user_ap = B.id_user_rq
    -&gt; WHERE B.id_user_rq IS NULL;
+------------+------------+
| id_user_rq | id_user_ap |
+------------+------------+
|          1 |          3 |
|          1 |          4 |
|          2 |          3 |
+------------+------------+
3 rows in set (0.00 sec)

mysql&gt;


Give it a Try !!!

mysql&gt; SELECT A.*
    -&gt; FROM default_relations_users A
    -&gt; LEFT JOIN default_relations_users B
    -&gt; ON  A.id_user_rq = B.id_user_ap
    -&gt; AND A.id_user_ap = B.id_user_rq
    -&gt; WHERE B.id_user_rq IS NULL
    -&gt; AND A.id_user_rq = 1;
+------------+------------+
| id_user_rq | id_user_ap |
+------------+------------+
|          1 |          3 |
|          1 |          4 |
+------------+------------+
2 rows in set (0.01 sec)

mysql&gt;