Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:25:30+00:00 2026-06-14T17:25:30+00:00

I have the following table in PostgreSQL database: CREATE TABLE maclist ( username character

  • 0

I have the following table in PostgreSQL database:

CREATE TABLE maclist (
    username character varying,
    mac macaddr NOT NULL,
    CONSTRAINT maclist_user_mac_key UNIQUE (username, mac)
);

I need a way to check if the MAC address is assigned to the user or the user has no assigned MAC addresses at all. Basically I need a query that returns row if all conditions are true or no condition is true ie NOT a XOR b.

EDIT:
Example:

username | mac
john     | 11:22:33:44:55:66
john     | 11:22:33:44:55:67
doe      | 11:22:33:44:55:68

If I query:

username = john, mac = 11:22:33:44:55:66                     -> true, 1 whatever...
username = john, mac != 11:22:33:44:55:66                    -> 0, null or nothing...
username = jane, mac = no matter what except john's or doe's -> true, 1 whatever...
username = jane, mac = john's or doe's                       ->  0, null or nothing...

I need true under two conditions:

  1. There is a row for that (user, mac) combination. There are no rows
  2. for that user AND there are no rows for that mac

So far I got this:

SELECT yes
FROM
  (SELECT 1 AS yes) AS dummy
LEFT JOIN maclist ON (username = 'user'
                      OR mac = '11:22:33:44:55:66')
WHERE ((username = 'user'
        AND mac = '11:22:33:44:55:66')
       OR (username IS NULL
           AND mac IS NULL);

It works, but it seems to me like a hack and I also have no idea about the performance of this query as the database grows.
My question is if there are any better ways to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:25:32+00:00Added an answer on June 14, 2026 at 5:25 pm

    EDIT: I’ve revised the logic slightly, I think it matches what you want

    The following code will return 1 under two conditions…

    • There is a row for that user and that mac
    • There are 0 rows for that user and there are 0 rows for that mac

    Under all other conditions, the query returns 0.

    • There are rows for that user but none of them are for that mac
    • There are rows for that mac but none of them are for that user

    SELECT
      CASE WHEN COUNT(*) = 0                                THEN 1
           WHEN SUM(CASE WHEN mac = '11:22:33:44:55:66'
                          AND user = 'user' THEN 1
                                            ELSE 0 END) = 1 THEN 1
                                                            ELSE 0
      END
    FROM
      maclist
    WHERE
         username = 'user'
      OR mac      = '11:22:33:44:55:66'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table and sequence in my postgresql-8.4 database: CREATE TABLE complexobjectpy
I have a PostGreSQL database with the following data model: CREATE TABLE staff (s_id
I have the following table in my postgreSQL 8.3.14 database timestamp status 2012-03-12 19:15:01
I have executed the following create statement using SQLWorkbench at my target postgresql database:
I have a a PostgreSQL database table with the following simplified structure: Device Id
I have the following table in PostgreSQL 8.4.5: snake=> create table gps ( id
I have the following table declared in MySQL: CREATE TABLE IF NOT EXISTS `ci_sessions`
I'd need advice on following situation with Oracle/PostgreSQL: I have a db table with
In PostgreSQL 8.3, let's say I have a table called widgets with the following:
I have the following table drop table names; create table names( name varchar(200), surname

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.