How to select columns that have name beginning with same prefix?
Using SQLITE, is there a way to select a set of columns that have name beginning with same prefix.
Suppose we have columns : PREFIX_col1, PREFIX_col2, ...
Is it possible to do a request like :
SELECT 'PREFIX_*' FROM mytable; Which of course doesn't work.
(2.1) By curmudgeon on 2021-08-22 19:23:42 edited from 2.0 in reply to 1 [link] [source]
It's easy enough to get the SQL using select 'select ' || group_concat(name) || ' from mytable' from pragma_table_info('mytable') where substr(name,1,7)='PREFIX_' but I think you'd need the eval function to execute the SQL which takes you into load extension territory https://sqlite.org/c3ref/load_extension.html I'm not sure if eval is automatically loaded into the CLI and I don't have access to it at the moment and to be honest I'm not sure if eval can return multiple rows. You might need a user defined function to execute the SQL. https://sqlite.org/c3ref/create_function.html
please could you make for me a good Query that can work with my IDE
i have this Table: [USERS] has 07 Fields:
- ID (PRIMARY kEY)
I need to Select just TWO Fields using Query like that:
|SELECT "SELECT "||GROUP_CONCAT(name)||" from USERS" "FROM PRAGMA TABLE_INFO('USERS')"|
what i mean Above is to load into my Query just this TWO Fields:
|SELECT "SELECT "||GROUP_CONCAT(name)||" from USERS"|
FROM PRAGMA_TABLE_INFO("USERS") WHERE SUBSTR(NAME,1,5)="USER." +++++++++ BUT Still not work for me: https://res.cloudinary.com/bravesofts/image/upload/v1629662772/PREFIX.png
How is that unreadable (and likely unworkable) glop of text better than:
"USER.NickName" as Nickname, "USER.Password" as Password
In other words, why go to the trouble of writing a longer and less readily comprehended query that requires an eval() step to work?
i understand your Solution Mr:Larry
but what about a query that require 10 or 20 Columns from 50 or 100 columns ?
do i need to write All Fields Names 10 or 20 times for every query ?
If you have a big pile of column names where meanings are encoded in portions of the names, then you would be better off devising a schema where that data can be incorporated into queries by less devious means.
Even with this naming issue aside, "50 or 100 columns" is a schema design smell. Such a table probably holds more than it should, so it would benefit from a pass or three of normalization.
You can do this in the CLI using .once and .read .once /tmp/aaa.sql select 'select ' || group_concat(name) || ' from mytable' from pragma_table_info('mytable') where substr(name,1,7)='PREFIX_'; .read /tmp/aaa.sql
Simple but very effective , thanks for this hint how to use the CLI for dyn. generated SQL.
If you read an IBM DB2 manual you will see that it describes hundreds of options. At the end, in a colored box, it says: IBM recommends not using any of these options. It is hilarious but it is an example of what are called "best practices". Perhaps SQLite could also give that kind of suggestion in its documentation.