简体   繁体   中英

How to Exclude a Table from Select Distinct

I'm going crazy...

I want to generate an array of tables that will need to pass through a loop, but I don't want the table "ClientData" included. Whatever I do, though, it keeps showing up in the array...

I've tried this:

SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('QuoteID') AND
COLUMN_NAME NOT REGEXP '\\([^\\)]*ClientData.*\\)'
    AND TABLE_SCHEMA='$db';

I've tried this:

SELECT DISTINCT TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='$db' OR
`COLUMN_NAME` LIKE 'Training%' OR 
`COLUMN_NAME` LIKE 'Development%' OR  
`COLUMN_NAME` LIKE 'Hardware%' OR 
`COLUMN_NAME` LIKE 'MobilityL%'

In this second case, I have tried to use the LIKE function to pull only the tables I need, but it STILL pulls ClientData. I've even added:

`COLUMN_NAME` NOT LIKE 'ClientData'

And every variation of 'Client%'...

I'm spent. Would appreciate any help anyone has.


vearutop,

You broke the dam for me, thanks! I was caught up in Column_name for some reason and should have been thinking Table_name the whole time. Embarrassing...

I cribbed your Table_name != 'ClientData' and replaced the Column_Name filters with Table_name and it's perfect. Here's the final:

SELECT DISTINCT TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='$db' OR
TABLE_NAME LIKE 'Training%' OR 
TABLE_NAME LIKE 'Development%' OR  
TABLE_NAME LIKE 'Hardware%' OR 
TABLE_NAME LIKE 'MobilityL%'  AND
TABLE_NAME != 'ClientData'

Using just TABLE_NAME != 'ClientData' wasn't a tight enough filter, as I also needed to filter out some other tables, but this is perfect. Thanks again. I'm in my 50th+ hour of consecutive coding to meet a deadline (need to quit volunteering for things!) and this gets me about 2 hours closer to some sleep.

SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('QuoteID') AND
TABLE_NAME != 'ClientData'
AND TABLE_SCHEMA='$db';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM