Inverse of a Query

Inverse of a Query



What would be the inverse of the following query:


SELECT * FROM `test_result`
INNER JOIN `perinfo` ON `test_result`.`mobileno` = `perinfo`.`mobileno`
WHERE `perinfo`.`insti_id` = '2'
AND `test_id` = (SELECT `test_id`
FROM `test`
WHERE `test_name` = 'One')



i want to select rows from database table 'perinfo' where insti_id is 2 and also they are present in data table 'test_result'




3 Answers
3



Try this:


SELECT * FROM `test_result` `t`
INNER JOIN `perinfo` ON `test_result`.`mobileno` = `perinfo`.`mobileno`
WHERE `perinfo`.`insti_id` = '2'
AND NOT EXISTS(SELECT 1 FROM `test`
WHERE `test_name` = 'One'
AND `test_id` = `t`.`test_id`)



you can change your filter uisng != (not equal) condition instead of = (equal)


!=


=


SELECT * FROM `test_result`
INNER JOIN `perinfo` ON `test_result`.`mobileno` = `perinfo`.`mobileno`
WHERE `perinfo`.`insti_id` != '2'
AND `test_id` != (SELECT `test_id`
FROM `test`
WHERE `test_name` = 'One')



but if you have more then a result from subquery you should is IN or NOT IN


SELECT * FROM `test_result`
INNER JOIN `perinfo` ON `test_result`.`mobileno` = `perinfo`.`mobileno`
WHERE `perinfo`.`insti_id` != '2'
AND `test_id` NOT IN (SELECT `test_id`
FROM `test`
WHERE `test_name` = 'One')



I don't understand what you mean by "the inverse of a query", but to obtain the results you describe in the question's text you'd want to use


SELECT *
FROM `perinfo`
INNER JOIN `test_result`
ON `test_result`.`mobileno` = `perinfo`.`mobileno`
WHERE `perinfo`.`insti_id` = '2'



In other words, just reverse the positions of perinfo and test_result in the query, and remove the sub-select on test because it's not specified as part of the desired results.


perinfo


test_result


test






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ