[英]SHOW COLUMNS FROM Goods WHERE condition AND [there is at least one value in the column]
我的桌子上有很多不同的列,例如
resolution, brightness, type, width, camera_type, projection_type etc
我只需要獲取與諸如LED電視之類的過濾器相關的那些列的名稱,這意味着我需要某種方式
SHOW COLUMNS FROM Goods WHERE category_id = 5 AND [there is at least one value in the column for a row that meets the category_id = 5 condition]
可能嗎
您可以在要獲取的行上放置條件,where子句對此負責。 所需的列僅在select語句中指定,無法指定要選擇的列。 獲得結果后,您可以根據值決定使用哪些列。
我猜你想要的是具有category_id = 5的非空值的列名。這里的困難是找出哪些類別具有category_id = 5的非空值。這必須在一個每列,但可以做到。
說實話...我不在MySql中工作,但是我敢肯定您可以按照要求進行任何調整。
-- create a temp table to hold the results
CREATE TEMPORARY TABLE TempTable (columnName varchar(100));
-- create a bunch of dynamic SQL to insert its name into the temp table when category_id = 5 and its value is not null
DECLARE column_name VARCHAR(100);
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
-- Declare the cursor
DECLARE columns_cur CURSOR FOR
SELECT
column_name
FROM information_schema.columns
WHERE TABLE_NAME = 'Goods';
-- Declare 'handlers' for exceptions
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN columns_cur;
select FOUND_ROWS() into num_rows;
the_loop: LOOP
FETCH columns_cur
INTO column_name;
IF no_more_rows THEN
CLOSE columns_cur;
LEAVE the_loop;
END IF;
PREPARE stmt FROM 'IF(EXISTS(SELECT * FROM Goods WHERE ? IS NOT NULL AND catetory_id = 5)) INSERT INTO TempTable (column_name) VALUES ('?');'
EXECUTE stmt USING @column_name;
DEALLOCATE PREPARE stmt;
END LOOP the_loop;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.