If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Problem selecting records using column_name with %

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-07-07, 11:33
AQG AQG is offline
Registered User
 
Join Date: Aug 2007
Posts: 2
Problem selecting records using column_name with %

Hello, I'm new to MySQL and will appreciate any help

SELECT COLUMN_NAME FROM information_schema.`COLUMNS` C WHERE table_name='disk_ses_app1' AND COLUMN_NAME LIKE 'mntU%';

Gives me the column names from wich I need to obtain the records:
+------------------+
| COLUMN_NAME |
+------------------+
| mntU_1_var |
| mntu__var |
| mntU_1_var |
| mntu__var |
+------------------+
But I need to obtain all the records from the table using the Column_name and don't know where to go from here???

Any Ideas???
Thanks
Reply With Quote
  #2 (permalink)  
Old 08-07-07, 11:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
you need to select the TABLE_SCHEMA as well as the COLUMN_NAME

looks like you have the same table in more than one database
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-07-07, 11:48
AQG AQG is offline
Registered User
 
Join Date: Aug 2007
Posts: 2
Thanks,
But as you might see I'm new tho MySQL could you please give me an example, I'ved looked around and have found nothing.

Regards
Reply With Quote
  #4 (permalink)  
Old 08-10-07, 05:43
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
You're going to need a stored procedure which contains a cursor which does a union between all the select column from tables

e.g.


Code:
CREATE PROCEDURE build_tables_results()
BEGIN
  DECLARE cur1 CURSOR FOR SELECT COLUMN_NAME,TABLE_NAME FROM information_schema.`COLUMNS` C WHERE table_name='disk_ses_app1' AND COLUMN_NAME LIKE 'mntU%';
  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
  DECLARE a,b VARCHAR(255);

  OPEN cur1;
  REPEAT
    FETCH cur1 INTO a, b;
    SELECT a FROM b;
  UNTIL done END REPEAT;

  CLOSE cur1;
END
Reply With Quote
  #5 (permalink)  
Old 08-10-07, 05:44
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
By the way, my answer above does NOT contain the UNION part for the resultset. I'm uncertain how to achieve this (without doing further work).
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On