6

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!

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Snuur
  • 311
  • 1
  • 3
  • 11
  • By does not work do you mean the query doesn't fire at all or do you mean that it doesn't return any results? – TangoKilo Aug 22 '13 at 09:52
  • 1
    `LIKE \`pweb\`` should be `LIKE 'pweb%'`. String literals aren't back ticked. – Martin Smith Aug 22 '13 at 09:53
  • Your question needs to be clarified - do you mean pweb1, pweb2, pweb3 for example ? Or do you mean pweb and the column has a value of integer. You have 2 answers below for each. – SteveB Aug 22 '13 at 09:59

3 Answers3

11

Try this

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_NAME`='producten' AND `COLUMN_NAME` LIKE 'pweb%'
AND DATA_TYPE = 'int'
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
  • this did the trick, thank you, I did not know the % had to be added, LIKE has worked without for me in other instances. – Snuur Aug 22 '13 at 10:37
3

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

user2232273
  • 4,898
  • 15
  • 49
  • 75
1
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME='producten' 
AND COLUMN_NAME LIKE 'pweb%'
AND DATA_TYPE = 'int'
juergen d
  • 201,996
  • 37
  • 293
  • 362