Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select column names that start with x [duplicate]

Tags:

mysql

wildcard

Im trying to select all column names from a mysql table that start with pweb and then have an integer.

This works for all column names:

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_NAME`='producten';

But this does not work unfortunately, how can I make this happen?

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_NAME`='producten' AND `COLUMN_NAME` LIKE `pweb`;

Thanks!

like image 276
Snuur Avatar asked Aug 22 '13 09:08

Snuur


People also ask

Why is R adding X to column names?

The reason for this is that the column names of these columns contained numbers and special characters at the beginning of the column name. The first two column names started with a number and hence the R programming language added the prefix X.

What is the difference between select * and select column name?

The reason I use SELECT COLUMN_NAMES is when using Stored Procedure, adding columns to the table will not screw your application. select * will give additional column (which you've just added to the table) and application will get additional column and may raise error.

What is duplicate column name in SQL?

If you have a list that contains two or more columns with similar names, you can get this error: Duplicate column name detected: List: "a list name", Internal column name: "a column name", SQL Column name: "a column name".


2 Answers

Try this

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_NAME`='producten' AND `COLUMN_NAME` LIKE 'pweb%'
AND DATA_TYPE = 'int'
like image 140
Padmanathan J Avatar answered Sep 27 '22 21:09

Padmanathan J


you have to add the % after pweb.

so the sql statement select all column_name which begins with pweb**

Note: ** means other caracteres

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA, COLUMNS 
WHERE TABLE_NAME='producten' AND COLUMN_NAME LIKE 'pweb%';

Hope this help you

like image 42
user2232273 Avatar answered Sep 27 '22 20:09

user2232273