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

  • Home
  • SEARCH
  • 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 7555545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:38:17+00:00 2026-05-30T11:38:17+00:00

I have a big giant sql query where I select and format a bunch

  • 0

I have a big giant sql query where I select and format a bunch of rows for export into Excel. And I want to add one more role, pretty much if the rid (role id) EXISTS and IS 3 then set it to be Role 1 otherwise set it to be Role 2.

Mysql Query Currently

SELECT uc_orders.billing_first_name AS 'First Name', uc_orders.billing_last_name AS 'Last 
Name', users.name AS 'Username', uc_orders.billing_street1 AS 'Address', 
uc_orders.billing_city AS 'City', uc_zones.zone_name AS 'State',uc_orders.billing_postal_code
 AS 'Postal Code', ROUND(uc_orders.order_total,2) AS 'Order Total'  FROM uc_orders, users,
 uc_zones WHERE uc_orders.uid!='0' AND uc_orders.uid = users.uid AND uc_orders.billing_zone =
 uc_zones.zone_id AND uc_orders.modified BETWEEN '1271124575' AND '1274978899'

HOWEVER the user will not ALWAYS exist in the users_role database (it only exists if it is 3 or some other number, normally it will not exist). So if it does not exist, it would also return ‘Role 2’;

So

  IF users.uid EXISTS IN users_role.uid THEN
    IF users_role.uid EQUALS users.uid AND users_role.rid = 3
      THEN
        return 'Role 1'
      ELSE
        return 'Role 2';
  ELSE return 'Role 2';

How can I do this in MySQL? Or would PHP be the only way?

  • 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-30T11:38:18+00:00Added an answer on May 30, 2026 at 11:38 am

    You should be using JOIN for multi-table queries. Also consider formatting your query and using aliases for easier reading.

    SELECT
        `a`.`billing_first_name` as `First Name`,
        `a`.`billing_last_name` as `Last Name`,
        `b`.`name` as `Username`,
        `a`.`billing_street1` as `Address`,
        `a`.`billing_city` as `City`,
        `c`.`zone_name` as `State`,
        `a`.`billing_postal_code` as `Postal Code`,
        ROUND(`a`.`order_total`,2) as `Order Total`,
        IF(`d`.`rid` IS NOT NULL AND `d`.`rid`=3,'Role 1','Role 2') as `Role`
    FROM `us_orders` as `a`
    JOIN `users` as `b` ON `a`.`uid`=`b`.`uid`
    JOIN `uc_zones` as `c` ON `a`.`billing_zone`=`c`.`zone_id`
    LEFT JOIN `users_role` as `d` on `a`.`uid`=`d`.`uid`
    WHERE `a`.`modified` BETWEEN '1271124575' AND '1274978899'
    

    The part that answers your actual question is the line with the IF and the one with the LEFT JOIN.

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

Sidebar

Related Questions

Hi there I have big problem with one select query There is a table:
I have big xls file with formulas and graphics. I want to add some
I have big problems with stopping a service, which plays audio file. I want
I have big process running. It spawns two threads. I want to debug those
We have a giant big ass database and we can't visualize it. Is there
I have the following code: <?php $FILE=giant-data-barf.txt; $fp = fopen($FILE,'r'); //read everything into data
I have big query based datamodel, and I wish to display results of Linq
Im programming C# WinForm application which have big SQL Server Database. I need to
see i have written one program for big endian now i dont have big
I have a server out in the big bad internets and I want to

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.