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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:03:23+00:00 2026-06-16T18:03:23+00:00

I have the following SQL query: $sql = SELECT id, refid, action_id, action_type, co_user_id,

  • 0

I have the following SQL query:

$sql =  "
    SELECT
        id,
        refid,
        action_id,
        action_type,
        co_user_id,
        cust_vend_id,
        cust_vend_type,
        Aes_decrypt(cust_vend_name, '".DBKEY."') AS cust_vend_name,
        Aes_decrypt(amount, '".DBKEY."')         AS amount,
        Aes_decrypt(action_date, '".DBKEY."')   AS action_date,
        Aes_decrypt(memo, '".DBKEY."')         AS memo,
        Aes_decrypt(trans_id, '".DBKEY."')     AS trans_id,
        entry_datetime,
        part_id,
        polarity
    FROM
        generated_actions
    WHERE  acc_type = 1
        AND acc_id = $ref_id
        AND action_type != 2
        AND reverse_id IS NULL
    ORDER BY generated_actions.action_id DESC ";

It works, but I need to add an INNER JOIN ON either customers or vendors depending on whether the contents of cust_vend_type column is a ‘1’ (customers) or ‘2’ (vendors) and then grab customers_comp_name if ‘1’ or `vendors_comp_name’ if ‘2’.

Looking forward to choosing the best solution or marking up helpful advice!

UPDATE

This is the code updated with @ciaran’s response below. The problem is that it seems to be returning an empty resultset even though I know there are qualified records.

$sql =  "
    SELECT
        ga.id,
        ga.refid,
        ga.action_id,
        ga.action_type,
        ga.co_user_id,
        ga.cust_vend_id,
        ga.cust_vend_type,
        Aes_decrypt(ga.amount, '".DBKEY."')                 AS amount,
        Aes_decrypt(ga.action_date, '".DBKEY."')            AS action_date,
        Aes_decrypt(ga.memo, '".DBKEY."')                   AS memo,
        Aes_decrypt(ga.trans_id, '".DBKEY."')               AS trans_id,
        ga.entry_datetime,
        ga.part_id,
        ga.polarity,
    SELECT CASE
        WHEN
            ga.cust_vend_type IS NULL
        THEN
            NULL
        WHEN
            ga.cust_vend_type = '1'
        THEN
            AES_DECRYPT(c.cust_comp_name, '".DBKEY."')
        ELSE
            AES_DECRYPT(v.vendor_comp_name, '".DBKEY."') END AS cust_vend_name
        FROM
            generated_actions ga
        LEFT OUTER JOIN
            customers c
        ON
            c.cust_id = ga.cust_vend_id
        LEFT OUTER JOIN
            vendors v
        ON
            v.vendor_id = ga.cust_vend_id
        WHERE
            ga.acc_type = 1
        AND
            ga.acc_id = $ref_id
        AND
            ga.action_type != 2
        AND
            ga.reverse_id IS NULL
        ORDER BY
            ga.action_id DESC ";

Not sure what I’ve done wrong?

  • 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-16T18:03:24+00:00Added an answer on June 16, 2026 at 6:03 pm

    You need an outer join and then use an CASE (or IF) to select the correct name…

    SELECT CASE WHEN generated_actions.cust_vend_type Is Null Then Null 
                WHEN generated_actions.cust_vend_type = '1'
                THEN customers.customers_comp_name
                ELSE vendors.vendors_comp_name END as Cust_Vend_Name
    
      FROM generated_actions
    
           LEFT OUTER JOIN customers ON customers.id=generated_actions.cust_vend_id
           LEFT OUTER JOIN vendors ON vendors.id=generated_actions.cust_vend_id
    

    i.e. your query should read…

    $sql =  "
        SELECT
            ga.id,
            ga.refid,
            ga.action_id,
            ga.action_type,
            ga.co_user_id,
            ga.cust_vend_id,
            ga.cust_vend_type,
            Aes_decrypt(ga.amount, '".DBKEY."')                 AS amount,
            Aes_decrypt(ga.action_date, '".DBKEY."')            AS action_date,
            Aes_decrypt(ga.memo, '".DBKEY."')                   AS memo,
            Aes_decrypt(ga.trans_id, '".DBKEY."')               AS trans_id,
            ga.entry_datetime,
            ga.part_id,
            ga.polarity,
            CASE WHEN ga.cust_vend_type IS NULL THEN NULL
                 WHEN ga.cust_vend_type = '1' THEN AES_DECRYPT(c.cust_comp_name, '".DBKEY."')
                 ELSE AES_DECRYPT(v.vendor_comp_name, '".DBKEY."') END AS cust_vend_name
       FROM generated_actions ga
            LEFT OUTER JOIN customers c ON c.cust_id = ga.cust_vend_id
            LEFT OUTER JOIN vendors v ON ga.cust_vend_id
      WHERE ga.acc_type = 1
        AND ga.acc_id = $ref_id
        AND ga.action_type != 2
        AND ga.reverse_id IS NULL
      ORDER BY ga.action_id DESC ";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following SQL query: SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID, ra.BEZEICHNUNG
I have the following SQL query SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID, ra.BEZEICHNUNG
Let's say I have the following SQL query SELECT id, name, title, description, time
I have the following SQL Server query: SELECT area FROM places WHERE REPLACE(area,'-',' ')
I have the following which works in SQL Query Analyzer . select oh.* from
In sql server 2008, I have the following query: select c.title as categorytitle, s.title
I have a SqlDependency set up using the following query: string sql = "SELECT
i have the following sql query select * from tblArea where AreaDescription in ('Processing1','Geology
I have the following SQL query: select AuditStatusId from dbo.ABC_AuditStatus where coalesce(AuditFrequency, 0) <>

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.