I have a table like that :
id exemple_ABC tr_ABC exemple_BCD tr_BCD ....
I want to SELECT all of the column beginning by "exemple_" I try :
SELECT exemple_* FROM myTable
or
SELECT exemple_% FROM myTable
But all of this doesn't work...
I have a table like that :
id exemple_ABC tr_ABC exemple_BCD tr_BCD ....
I want to SELECT all of the column beginning by "exemple_" I try :
SELECT exemple_* FROM myTable
or
SELECT exemple_% FROM myTable
But all of this doesn't work...
You cannot with standard SQL. Column names are not treated like data in SQL.
If you use a SQL engine that has, say, meta-data tables storing column names, types, etc. you may select on that table instead.
try this one
select * from information_schema.columns
where table_name='table1' and column_name like 'exemple_%'
You have to create a dynamic query thanks to the information_schema
table.
SELECT
CONCAT(
'SELECT ',
SUBSTRING_INDEX(
GROUP_CONCAT(CONCAT('`', column_name, '`') ORDER BY column_name),
',',
2),
' FROM your_table'
)
FROM
information_schema.columns
WHERE
table_schema=DATABASE()
AND table_name ='your_table'
AND column_name LIKE 'your_prefix_%'
INTO @sql;
PREPARE stmt FROM @sql;
EXECUTE stmt;
Have a look here (where I have found this query I have changed a little bit): select first N columns of MySQL table
It's a pretty strange situation not knowing the exact database design, but basically your only option is to wildcard ALL columns and choosing which you want on the client side, i.e:
SELECT * FROM myTable