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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:31:04+00:00 2026-05-25T13:31:04+00:00

I have this log table in MySQL with columns ActionName and SourceName. The same

  • 0

I have this log table in MySQL with columns ActionName and SourceName.

The same actions can be registered multiple times from different sources.

So an example table might look like

ActionName    SourceName
----------------------------
Add           S01
Add           S02
Add           S02
Edit          S01
Edit          S01
Delete        S01
Delete        S02

Now I would like to query this table and find the actions which have been performed by both S01 and S02. So the results would be:

 ActioName
--------------
Add
Delete

How would I solve this with SQL?

  • 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-25T13:31:04+00:00Added an answer on May 25, 2026 at 1:31 pm

    A specific answer…

    SELECT
      ActionName
    FROM
      yourTable
    WHERE
      SourceName in ('S01', 'S02')
    GROUP BY
      ActionName
    HAVING
      COUNT(DISTINCT SourceName) = 2
    

    Possibly faster for your specific question…

    SELECT
      a.SourceName
    FROM
      yourTable  AS a
    INNER JOIN
      yourTable  AS b
        ON a.ActionName = b.ActionName
    WHERE
          a.SourceName = 'S01'
      AND b.SourceName = 'S02'
    

    A general answer…

    SELECT
      ActionName
    FROM
      yourTable
    INNER JOIN
      tableWithSourceNames
        ON yourTable.SourceName = tableWithSourceNames.SourceName
    GROUP BY
      ActionName
    HAVING
      COUNT(DISTINCT yourTable.SourceName) = (SELECT COUNT(DISTINCT SourceName) FROM tableWithSourceNames)
    

    It turns out that this scales very badly though (as your table increases in size, performance plummets). You can make an optimisation though…

    By holding a bit of meta-data about how selective each SourceName is…

    CREATE TABLE sourceNameMetaData (
      sourceName  VARCHAR(64),
      occurances  INT
    )
    

    I’d recommend keeping this talbe up to date with a trigger or something. You can then filter your ActionTable by the entry that is most restrictive, and then do the rest of the logic as normal.

    SELECT
      yourTable.ActionName
    FROM
    (
      SELECT
        ActionName
      FROM
      (
        SELECT
          sourceName
        FROM
          sourceNameMetaData
        INNER JOIN
          tableWithSourceNames
            ON tableWithSourceNames.SourceName = sourceNameMetaData.SourceName
        ORDER BY
          occurances ASC
        LIMIT
          1
      )
        AS filter    
      INNER JOIN
        yourTable
          ON yourTable.SourceName = filter.SourceName
      GROUP BY
        ActionName
    )
      AS filter
    INNER JOIN
      yourTable
        ON yourTable.ActionName = filteredData.ActionName
    INNER JOIN
      tableWithSourceNames
        ON yourTable.SourceName = tableWithSourceNames.SourceName
    GROUP BY
      yourTable.ActionName
    HAVING
      COUNT(DISTINCT yourTable.SourceName) = (SELECT COUNT(DISTINCT SourceName) FROM tableWithSourceNames)
    

    Notes:

    • This optimisation isn’t needed for small tables
    • This optimisation assumes you have indexes on BOTH (sourceName, ActionName) AND (actionName, sourceName)
    • It’s a brilliant example I use to show that more code CAN be faster
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this set up to log the amount of failed login attempts from
We have a table on mysql to log all visitors of our site. The
I have this code that fetches data from mySQL and outputs it as an
I have a log table that contains job status. One of the columns is
basically i have this problem i have a table of posts(in my mysql database)
I have a database table for log messages and at any time there can
I have this: public string Log { get { return log; } protected set
I have this code.. String you = buf.readLine(); Log.d(STRING, ikaw); StringTokenizer st = new
this is my code and have a error: $('#map_canvas').mouseover(function(e){ console.log(e.offset().left+' '+e.offset().top) }) thanks i
I have a internal website that users log into. This data is saved as

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.