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 7174761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:07:35+00:00 2026-05-28T16:07:35+00:00

Lets say I have a table with just two columns: name and mood .

  • 0

Lets say I have a table with just two columns: name and mood. A row holds a persons name, and their mood, if they have multiple moods, then multiple rows are stored in the DB.

For example, in the database is John, who is happy, excited, and proud.

This is represented as

John Happy
John Excited
John Proud

What I want to do is select the name based on several moods being met. Similiar to the UNION:

SELECT name WHERE mood=Happy
UNION
SELECT name WHERE mood=Excited
UNION
SELECT name WHERE mood=Proud

However using the above union would result in:

John
John
John

Union all would result in one single result of John, but it would also select any names that only match one of the queries.

I can think of a few algorithms which would take each individual mysql_result resource (I’m using PHP), and look for matching results, but what I want to know is whether MySQL already facilitates this.

I realise the above is quite a vague generalisation, needless to say my actual program is alot more complicated and I’ve dumbed it down a little for this question, please don’t hesitate to ask questions.

  • 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-05-28T16:07:36+00:00Added an answer on May 28, 2026 at 4:07 pm

    Provided you have no duplicates, you can do it with a subquery:

    SELECT `name` FROM (
        SELECT `name`, COUNT(*) AS `count` FROM `moods`
        WHERE `mood` IN ('Excited', 'Happy', 'Proud') GROUP BY `name`
    ) WHERE `count` = 3
    

    Alternatively, you can use join:

    SELECT `m1`.`name` FROM `moods` `m1`
    JOIN `moods` `m2` USING (`name`)
    JOIN `moods` `m3` USING (`name`)
    WHERE `m1`.`mood` = 'Excited' AND `m2`.`mood` = 'Happy' AND `m3`.`mood` = 'Proud'
    

    Not so cute, but might be faster if you use LIMIT. Or maybe not. Depends a lot on query planner.

    UPD: thanks to Tudor Constantin for reminding me about HAVING, the first query can then be:

    SELECT `name` FROM `moods`
    WHERE `mood` IN ('Excited', 'Happy', 'Proud')
    GROUP BY `name`
    HAVING COUNT(*)>3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a table with columns such as: ID Name City State
Lets say I have a table with the following columns: Qty, INTEGER SaleDate, DATETIME
Lets say I have an Areas Table ( id, parentId, name, path ). Given
Lets say I have two MySQL databases with some complex table structures. Neither database
If I have a table TABLE_1 with, lets say 5 columns: COL1 | COL2
I have two tables lets say employees and order, both table have millions of
Lets say I have a table in a sql server 2000 database called TransactionType:
Lets say I have one table called REVIEWS This table has Reviews that customers
Lets say I have a table in MySQL called articles, that has a column
Lets say I have a table: -------------------------------------- | ID | DATE | GROUP |

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.