How to get the items not found in a Mysql table [duplicate] - mysql

This question already has answers here:
SQL IN Clause - get back IN elements that did not match
(5 answers)
Closed 7 months ago.
I have a random input of identifiers (primary key), and I need to return the identifiers which were not found in the mySql table.
Lets say my identifier list is ['FEB221571','1221SC170','75960620447200111', 'ABC12344'].
My table has the first three identifiers so I can find them on the table but I wish the last one to be returned as it cannot be found in the table. How do I implement that?

select * from identifier_list a
where not exists (select 'x' from some_table b and a.id = b.id);

Related

Is there a way where i can find the missing numbers from 1 table to another table? (Comparison) [duplicate]

This question already has answers here:
mysql opposite of inner join
(3 answers)
Closed 2 years ago.
is there a query where I can compare 2 table datas? For example.
My first table has ID 1,2,3,4,5,6,7,8,9,10. My 2nd table has ID 1,3,5,7,9.
My target is to get the missing values. So my expected output is 2,4,6,8,10.
Thank you in advance
Try this:
SELECT
num
FROM
first_table t1
WHERE
num
NOT IN
(SELECT
num
FROM
second_table
);

Mysql Inner Joın At least one match is required [duplicate]

This question already has answers here:
How to select rows with no matching entry in another table?
(11 answers)
How to return rows from left table not found in right table?
(7 answers)
Selecting all items in one table and join with another table, allowing nulls
(4 answers)
Closed 3 years ago.
I created two tables. I keep the topics in one and the comments in this topic in the other.
When I want to view the topics, I want to see the total number of comments on the topic from the comments table.
But if there is not at least one comment in the query I wrote, it does not bring results. How can I see my topics even if there is no comment on my Inner Join query?
Thank you.
Here is my Query:
SELECT portal_topics.id, portal_topics.topicDetail, portal_topics.topicName,portal_topics.topicCategory,portal_topics.topicAuthor, portal_topics.topicTıme, portal_topics.topicAvatar,portal_topics.topicViews, portal_topics.topicLikes, COUNT(portal_comments.id) as totalCommentCount FROM `portal_topics`
INNER JOIN portal_comments ON portal_comments.topic_id = portal_topics.id
WHERE portal_comments.topic_id = portal_topics.id
GROUP BY portal_topics.topicName
Note: The query does not contain any Syntax errors. I have arranged the column names in English for everyone to understand. I may have made a mistake in this section.

Method to use LEFT JOIN with columns with the same name [duplicate]

This question already has an answer here:
MySQL JOIN tables with duplicate column names
(1 answer)
Closed 6 years ago.
I am using SELECT SQL_CALC_FOUND_ROWS *, but I need to left join a table that has a column with the same name as the first one.
$sql = "
SELECT SQL_CALC_FOUND_ROWS *
FROM {$this->_db}
LEFT JOIN $this->_db2 ON $this->_db2.calc_id = $this->_db.calc_id
";
Any idea how to make this work, as now the value from the 2nd table is overwriting the fist one?
Since you are joining a table with a field with the same name as another, you cannot use SELECT *.
You need to manually list the fields that you want in the SELECT statement.

how do I delete reverse duplicates from a table [duplicate]

This question already has answers here:
selecting distinct pairs of values in SQL
(3 answers)
Closed 6 years ago.
I have a table with related values. Sometimes there are records with the reverse relation. I want to delete those but dont know how to handle this. eg:
table with two columns (KOL1 & KOL2):
A-B
A-C
A-D
A-E
B-C
B-E
F-G
C-A
C-D
In the example above C-A is the record I want deleted because it is the reverse record of A-C already in the list.
How can I do this using access QUERIES?
ok,I think this does the job
SELECT E1, E2
FROM Table1
WHERE E1<=E2
UNION
SELECT E2, E1
FROM Table1
WHERE E1>E2

Update entire table based on parameter of another table in a matching field [duplicate]

This question already has answers here:
MySQL syntax for Join Update
(2 answers)
Closed 8 years ago.
I've my table user_data that has over 8k records. Then I have my table Geo_location. Both tables join on the member_num column
Geo_location has a column called public that should match the table user_data column acceptinClients. Its an INT 0 or 1
I am having some data consistency problem and not all record match. I know how to do a long winded fetch and do one by one update in PHP if they dont match.
But I was wondering if there is a way to create a SQL query that will loop through all records in Geo_location - public and update them with whatever value is in user_data - acceptinClients
Is there a way to do that or am i asking too much? if so, how?
i have been reading around but cant find a simple solution for my problem or dont understand them
I don't know if I understand the problem, but isn't
UPDATE Geo_location AS g
JOIN users_data AS u ON u.member_num = g.member_num
SET g.public = ud.acceptinClients
correct?