Volodymyr Bobyr
Volodymyr Bobyr

Reputation: 424

SELECT multiple columns using a key / index / something else

Lets say I have a table with the following columns:

Let's say I very often fetch first_name, last_name, and height together when I'm fetching public data, so I want to bundle them together

Similarly, I want to bundle social_security_number and phone_number as private data.

In order to simplify my queries and not make a mistake in the future, I want to create some sort of a multi-column reference that will serve as an alias to public and private data, so:

I want to do something like this:

(made up syntax)

ALTER TABLE acc CREATE BUNDLE first_name, last_name, height AS public_acc_columns;
ALTER TABLE acc CREATE BUNDLE social_security_number, phone_number AS private_acc_columns;

And then use it like this

 -- returns first_name, last_name, height
SELECT public_acc_columns FROM acc;

 -- returns social_security, phone_number
SELECT private_acc_columns FROM acc;

 -- returns all columns
SELECT public_acc_columns, private_acc_columns FROM acc;

Upvotes: 0

Views: 43

Answers (1)

S. Walker
S. Walker

Reputation: 2162

I would advise creating two SQL Views. Setup one as acc_public and one as acc_private. Although I don’t know that the private would be needed.

Then you could say:

Select * from acc_public

Or

Select * from acc_private

Without seeing all your data structure, I can’t say the syntax you would need for the view, but here is some general information: https://www.w3schools.com/sql/sql_view.asp

Upvotes: 2

Related Questions