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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:53:08+00:00 2026-06-01T17:53:08+00:00

This is my database table. How would i be able to select the bottom

  • 0

This is my database table. How would i be able to select the bottom table from the database table?

Is it possible to create NameID and TypeID from unique names and types?

EDIT—
Ive got the type to work using case, since there are only 3 types.

SELECT 
  name, type, spending, 
    CASE type 
       WHEN 'Credit Card' THEN 1 
       WHEN 'Cash' THEN 2 
       WHEN 'Cheque' THEN 3 
    END AS typeID_  
FROM spendtable

Would it be possible to do the same on the name column?

Thanks!

—-database table—-

Name   Type        
     Spending
John     Credit Card   550
Brian    Cash  
          200
Frank   Cheque  
      600
John     Cash        
    150
John
    Cash             300
Frank   Credit
Card   200

—-table needed —-

NameID   Name   TypeID   Type  
            Spending
1
            John      1           Credit Card
    550
2
            Brian     2
          Cash               200
3             Frank    3           Cheque  
        600
1             John      2           Cash 
             150
1             John      2           Cash  
            300
3             Frank    1           Credit Card     200

——-EDIT

I ended up using 2 queries and php to get the required table

 function getInfo(){   
     SELECT 
       name, type, spending, 
         CASE type 
            WHEN 'Credit Card' THEN 1 
            WHEN 'Cash' THEN 2 
            WHEN 'Cheque' THEN 3 
         END AS typeID_  
     FROM spendtable 
 }

 function getName(){   
       SELECT name    
       FROM spendtable   
       GROUP BY share_ ASC           
 }


$info_array = $this->getInfo(); 
$name_array = $this->getName(); 

 for($i=0; $i<count($info_array); $i++){   
   $info_array[$i]['nameID'] = '';   
     for($j=0; $j<count($name_array); $j++){
     if($info_array[$i]['nameID'] == $name_array[$j]){
       $info_array[$i]['nameID'] = $j;
     }   
   }         
}
  • 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-01T17:53:10+00:00Added an answer on June 1, 2026 at 5:53 pm

    I was able to expand your existing query to support the NameID column:

    SELECT 
      name, type, spending, 
        CASE type 
           WHEN 'Credit Card' THEN 1 
           WHEN 'Cash' THEN 2 
           WHEN 'Cheque' THEN 3 
        END AS TypeID, 
        CASE name 
           WHEN 'John' THEN 1 
           WHEN 'Brian' THEN 2 
           WHEN 'Frank' THEN 3 
        END AS NameID
    FROM spendtable
    

    As a design limitation, people stored in your database are only allowed to be named John, Brian, or Frank 🙂

    Getting serious, assuming there are an unknown possible number of names I’m not sure you can (or would want to) do that kind of processing in the query. Instead, consider maintaining a separate Name table for keeping track of unique names:

    NameID   Name
    1        John
    2        Brian
    3        Frank
    

    Your query could then join on this table:

    SELECT 
      s.name, s.type, s.spending, 
        CASE s.type 
           WHEN 'Credit Card' THEN 1 
           WHEN 'Cash' THEN 2 
           WHEN 'Cheque' THEN 3 
        END AS TypeID, 
        n.nameid
    FROM spendtable s
    JOIN Name n on n.name = s.name
    

    Of course this would mean you needed to keep the Name table up to date, making sure to insert rows for new names.

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

Sidebar

Related Questions

I would like to be able to connect to a database with this table...
This would be an example of database table: PHP script should limit query result
Would you be able to confirm whether we can reference the other database.table like
I want to select data from a database table that is not part of
After dynamically creating a client-side table based on SELECT information retrieved from MySql database,
HI, I have a problem with this query SELECT * FROM table WHERE `name`
select * from table where category=@categoryid I'm not sure how easy is this but
enter code here My problem is this: in this database the junction table contains
i have database table like this +-------+--------------+----------+ | id | ip | date |
I use this code to update data in database table. Can reuse same code

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.