• 0

[MySQL] Get ONLY unique values from 2 tables?


Question

Hey guys,

Could somebody please take a look at the following problem for me please?

I have 2 tables, and from which I return 1 column from each...

ID ID

1 1

2 2

3 3

4 4

5 5

6

7

8

9

10

Is there a way I can run ONE query to return only the non-duplicated items? (6,7,8,9,10). Currently I'm returning both sets of results with 2 queries and running them through PHP.

Most, inefficient!

Cheers,

SilverB. :blush:

5 answers to this question

Recommended Posts

  • 0

An exclusion join:

SELECT DISTINCT a.id
FROM a LEFT JOIN b USING (id)
WHERE b.id IS NULL

More efficient than the subquery-comparison way...

The only potential pitfall is this (and the subquery method) relies on table a having more records than table b. So long as you know which has more this isn't a problem, but if you don't then a more complicated query is required in order to properly emulate MINUS.

Edited by mrbester
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Two ways information like this is passed on. 1. The developers release the "how-to" content and Neowin passes it on. 2. Neowin, or anyone else in the tech community figure out how to do it, Neowin passes it on. Which one are you?
    • On my PC, when using UWP, I have a recurring issue with the scroll bar disappearing after sending a message with a pasted image or text. The only way to fix it was to close and reopen WhatsApp. Now I'll still need this workaround to free up memory in WPA, since that's what I need to do when the WhatsApp tab is consuming 4 GB of RAM after being open for a long time.
    • It’s not just that malware can escape the sandbox but that having Windows Sandbox enabled leaves a vector for malware on the host to hide from AV solutions. The likelihood of this happening must be low but depends on your risk appetite. https://windowsforum.com/threa...x-for-cyber-attacks.356358/
    • https://www.windowsdigitals.com/how-to-remove-items-from-right-click-menu-in-windows-11/
    • Ironically, I do have audio engineering experience, but still find the different flavors of VB-Audio's VoiceMeeter (and more advanced options like Banana and Potato) to be a little unintuitive. Yes, I can figure them out, but despite having a UI that looks like an audio mixer, they don't exactly follow typical audio mixing conventions, so it takes a little time to figure out how the software works. Still, I feel like for what you are asking for, VoiceMeeter is a good solution. If you're interested, I could probably create a quick how-to video.
  • Recent Achievements

    • Rookie
      Snake Doc went up a rank
      Rookie
    • First Post
      nobody9 earned a badge
      First Post
    • One Month Later
      Ricky Chan earned a badge
      One Month Later
    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      497
    2. 2
      Michael Scrip
      205
    3. 3
      ATLien_0
      201
    4. 4
      Xenon
      138
    5. 5
      +FloatingFatMan
      117
  • Tell a friend

    Love Neowin? Tell a friend!