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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:43:41+00:00 2026-06-06T17:43:41+00:00

I have a table called LOGS with the following columns: id – user –

  • 0

I have a table called LOGS with the following columns:

id - user - object - ref - field - from - to - join - ts

to store logging of a PHP app I am writing. However, is it possible in the SQL query when I return all the data into PHP to do a ‘conditional’ join? For instance the join column might contain ‘people’ indicating that the field column needs to be joined in relationship with the table people.

Is this possible? Or shall I have to do it on the PHP side?

  • 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-06T17:43:44+00:00Added an answer on June 6, 2026 at 5:43 pm

    A LEFT join should do the trick here

    select * 
    from
        LOGS l
    left join 
        PEOPLE p on p.peopleid = l.field and l.join = 'people'
    

    I’m not sure I’ve used the correct relationship fields between LOGS and PEOPLE but by including a clause of join where log type is people then you can see PEOPLE entries are conditionally returned.

    Things become more complicated when you want to conditionally return from different tables, because you need to make sure the extra fields, brought in by the entity table are the same (or at least are identified as the same). In which case your forced to UNION results.

    select 
        l.*,
        p.peopleid as entityid, 
        p.fullname as displayname
    from
        LOGS l
    left join 
        PEOPLE p on p.peopleid = l.field and l.join = 'people'
    
    union all
    
    select 
        l.*,
        a.accountid as entityid, 
        p.accountname as displayname
    from
        LOGS l
    left join 
        ACCOUNT a on a.accountid = l.field and l.join = 'account'
    

    or perhaps this

    select 
        l.*,
        entity.entityid as entityid, 
        entity.displayname as displayname
    from
        LOGS l
    left join 
    (
        select 'people' as type, p.peopleid as entityid, p.fullname as displayname
        from PEOPLE
    
        union all
    
        select 'account', a.accountid, p.accountname
        from ACCOUNT
    ) entity on entity.type = l.join and entity.entityid = l.field
    

    But I can imagine combining lots of your entity tables like this to return logs could make for a really slow query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called tblRentalRates with the following columns: rate_id (int) rate_startdate (datetime)
i have table called as Support, which have a field named Name and contains
I have table called posts in my DB. Each post has field called social_network
I have a table called ProjectRegion. It has two columns, an id and a
I have a table called scheduler_sched which has several columns, including a column called
I have a log table with a date field called logTime. I need to
I have table called FinalForgotten which only contains one field called aname. The field
I have table called product review with columns productreviewid , productid , shopperid ,
I have table called location with a field area. This field can contain various
I have table called articles_tags which have two columns: article_id tag_id (has_and_belongs_to_many association) (I

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.