[英]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.