2017-02-01 2 views
-1

Das ist mein TischSql bekommen bestimmte Spalte aus derselben Tabelle

----------------------------------------------------------- 
id | tempname | col1 | col2 | col3 | col4 | col5 | col6 
----------------------------------------------------------- 
1 | 1  | on | off | off | on | off | off 
----------------------------------------------------------- 
2 |  2 | off | off| on | off | off | on 
----------------------------------------------------------- 

ich möchte Daten aus obiger Tabelle, deren Wert auf und ich möchte nicht eine andere Spalte i nur die Spalte möchten, die Wert auf

hat

Ergebnis: Das ist mein Tisch

----------------------------------------------------------- 
id | tempname | col1 | col3 | col4 | col6 
----------------------------------------------------------- 
1 | 1  | on | off | on | off 
----------------------------------------------------------- 
2 |  2 | off | on | off | on 
----------------------------------------------------------- 

i will keine zusätzlichen Spalten und ich wollte mit Select * Abfrage tun. Ich möchte den Spaltennamen in der Select-Abfrage nicht angeben

+0

entfernte ich die unvereinbar Datenbank-Tags. Bitte markieren Sie die Datenbank, die Sie wirklich verwenden. –

+0

mysql. Thankx gurv –

+0

Ich möchte so –

Antwort

0

Sie werden wahrscheinlich jede Spalte der Reihe nach betrachten müssen (mit Hilfe von information_schema-Spalten) und prüfen, ob Zeilen den 'on'-Wert haben und erstellen Sie dann eine benutzerdefinierte SQL-Anweisung aus den gefundenen Spalten, die mindestens eine Zeile mit diesem Wert enthalten.

Dieser Code unten funktioniert für SQL Server, Sie müssen es für die Verwendung mit mySql anpassen.

Sie könnten dann eine gespeicherte Prozedur von dieser erstellen und führen Sie dann nur, dass

NB. Der Code kann für sehr große Tabellen langsam sein, auch wenn der Teil, der überprüft, ob es irgendwelche „ons“ ist nur für jede Spalte für das erste Auftreten überprüfen gefunden (TOP mit 1)

DECLARE @sqlCountOn NVARCHAR (MAX) 
DECLARE @ParmDefinition NVARCHAR (MAX) 
DECLARE @colName nvarchar(50) 
DECLARE @countOn int 
DECLARE @colsToInclude NVARCHAR (MAX) 
DECLARE @query NVARCHAR(MAX) 
DECLARE @tableName NVARCHAR(50) 
DECLARE @columnWildcard NVARCHAR(50) 

-- define the columns you always want in the query results 
SET @colsToInclude = 'id,tempname' 
set @tableName='test' 
set @columnWildcard = 'col%' 

-- get a list of columns to check for on/off using a wildcard on the name 
DECLARE columnCursor cursor for SELECT COLUMN_NAME 
       FROM INFORMATION_SCHEMA.COLUMNS 
       WHERE TABLE_NAME = @tableName and COLUMN_NAME like @columnWildcard order by ordinal_position 
OPEN columnCursor 

FETCH NEXT FROM columnCursor INTO @colName 
--loop through all the columns that we want to look for on/off 
WHILE @@FETCH_STATUS = 0 
BEGIN 

     --build a query to count if we have *any* 'on's we have for this column 
     SET @sqlCountOn = 'select @retValOut= COUNT(*) from (select top 1 ' + @colName + ' from ' + @tableName + ' where ' + @colName + '=''on'')' 
     SET @ParmDefinition = N'@retvalOUT int OUTPUT'; 

     EXEC sp_executesql @sqlCountOn, @ParmDefinition, @[email protected] OUTPUT; 
     --check the count and if it's greater than 0 we want to include it, so add the column name to the output column list 
     if @countOn >0 set @[email protected] + ',' + @colName 

     FETCH NEXT FROM columnCursor INTO @colName 
END 

CLOSE columnCursor 
DEALLOCATE columnCursor 

--now create the query that will output just the columns we want from the table 
set @query='select ' + @colsToInclude + ' from [' + @tableName + '] ' 

EXEC SP_EXECUTESQL @query 
Verwandte Themen