1

I'm trying to select the rows not present in table B, based on table A.

Unlike table B, table A has "_00" at the end of the titleid, and the column is called title instead of titleid.

Table A:

id | titleid
---+----------
1  | TEST1_00
2  | TEST2_00
3  | TEST3_00
4  | TEST4_00

Table B:

id | title
---+-------
1  | TEST1
2  | TEST2

I currently have:

SELECT `t1.titleid`
FROM `tableb t1`
LEFT JOIN `tablea t2` ON `t2.title + '_00' = t1.titleid`
WHERE `t2.title` IS NULL

How can I select the values which are present in A but not in B?


Desired output

id | title
---+----------
3  | TEST3_00
4  | TEST4_00
7
  • You list table "A" first in the FROM and check for the match on table "B". Commented Mar 26, 2019 at 19:59
  • 1
    This is obviously an easily found duplicate. How to Ask Eg you could have googled your title. Commented Mar 26, 2019 at 21:27
  • Possible duplicate of How to select all records from one table that do not exist in another table? Commented Mar 26, 2019 at 21:32
  • @philipxy Not with the complexity of this question. Did you downvote the question aswell? Commented Mar 26, 2019 at 22:35
  • 1
    Given your comment you still don't seem to have researched much. This neither shows research effort nor is helpful. Your last line is a misstatement of your first line, and neither your first line nor the rest explains "based on". So--"unclear. (It's good you gave an example though.) See the downvote arrow mouseover text. PS Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. How to Ask minimal reproducible example Commented Mar 26, 2019 at 23:49

4 Answers 4

2
 SELECT t1.titleid
 FROM tablea t1
 LEFT JOIN tableb t2 ON t2.title + '_00' = t1.titleid
 WHERE t2.title IS NULL

You want to pull Data from Table A , do a left join on Table B and pull data where TableB.Title is null.

Your Query was trying to pull data where TableA.Title is NULL.

Sign up to request clarification or add additional context in comments.

1 Comment

This is misusing backquotes that you copied from the question where they were being misused.
2

It is possible to do this like that

SELECT `t1.titleid`
FROM `tablea t1`

WHERE 
NOT EXISTS (SELECT t2.title FROM `tableb t2` WHERE `t1.titleid = t2.title+ '_00'`)

1 Comment

Please before you post look at the formatted version of your post below the edit box. Read the edit help re inline & block formats for code & quotations. Please format your code reasonably. Read the edit help re code blocks. Also you have cut & pasted misuse of back quotes from the question. This is an example of why code questions need a minimal reproducible example & without one should be downvoted & closed, not answered.
1

You need to LEFT JOIN tableb instead if tablea

SELECT `t1.titleid`
FROM `tablea t1`
LEFT JOIN `tableb t2` ON `t1.titleid = t2.title+ '_00'`
WHERE `t2.title` IS NULL

This will show which records in tablea don't have a match in tableb and are null

Comments

0
select * from A where SUBSTRING(A.title,0, 6) in (select B.title from B )

2 Comments

Please before you post look at the formatted version of your post below the edit box. Read the edit help re inline & block formats for code & quotations. Please format your code reasonably. Read the edit help re code blocks. PS You are making assumptions about matching that the question does not give. Unclear questions should be downvoted & closed, not answered.
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.