从MySQL数据库中获取表名,其中列名称为X值等于Y.

时间:2017-11-24 09:48:56

标签: mysql

我想知道MySQL请求是否有办法获取列值等于特定值的表名。

例如,我有3个表:

Classroom1             Classroom2              Classroom3
+------+                +------+                 +------+
| name |                | name |                 | name |
+------+                +------+                 +------+
| Bob  |                | Dan  |                 | Mike |
+------+                +------+                 +------+
| Mark |                | Noa  |                 | Bob  |
+------+                +------+                 +------+

假设我想要有一个名叫'Bob'的学生的教室。我想得到,作为回应

['Classroom1','Classroom3']

MySQL中有类似内容吗?

提前致谢!!

修改

抱歉,我可能使用了错误的字词。我的意思是我想知道是否有一种方法可以获取MySQL请求以获取一个单元格值等于特定值的表名。

3 个答案:

答案 0 :(得分:1)

Check this question

SELECT TABLE_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME IN ('COLUMN_TO_FIND')
        AND TABLE_SCHEMA='YOUR_SCHEMA';

您还可以使用常规功能,例如(DistinctCount等)。 干杯!

答案 1 :(得分:1)

如果有人需要第一个

,我会添加另一个答案
  

首先,您必须找到与您想要的列匹配的表。

SELECT TABLE_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME IN ('COLUMN_TO_FIND')
        AND TABLE_SCHEMA='YOUR_SCHEMA';
  

现在,您想要使用特定列搜索每个表   内部的具体价值。

     

您需要创建一个迭代这些表的过程   按你的条款选择

DROP PROCEDURE IF EXISTS findName;

DELIMITER //

CREATE PROCEDURE findName()
BEGIN
  DECLARE _tablename VARCHAR(255);
  DECLARE _columnname VARCHAR(255);
  DECLARE _columnNameToFind VARCHAR(255) DEFAULT 'bob';

  DECLARE cur1 CURSOR FOR SELECT 
                             CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) AS table_name,
                            COLUMN_NAME AS column_name 
                            FROM INFORMATION_SCHEMA.COLUMNS
                            WHERE COLUMN_NAME IN ('YOUR_COLUMN_NAME')
                                AND TABLE_SCHEMA='YOUR_TABLE';

  OPEN cur1;

  read_loop: LOOP
    FETCH cur1 INTO _tablename, _columnname;

    SET @s = CONCAT('SELECT * FROM ', _tablename, ' WHERE ', _columnname =_columnNameToFind);
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

  END LOOP;

  CLOSE cur1;
END//

DELIMITER ;

CALL findName();
  

了解如何创建程序here

     

了解如何在程序here

内循环      

了解mysql中的游标如何工作here

答案 2 :(得分:0)

感谢l.g.karolos我能够得到我想要的东西。这是代码:

DELIMITER $$


DROP PROCEDURE IF EXISTS getStudentFromClassroom$$
CREATE PROCEDURE getStudentFromClassroom(student_name VARCHAR(255))
BEGIN
  DECLARE done INT DEFAULT FALSE;

  DECLARE _tablename VARCHAR(255);
  DECLARE _columnname VARCHAR(255) DEFAULT 'device';
  DECLARE _columnNameToFind VARCHAR(255) ;


   DECLARE cur1 CURSOR FOR SELECT 
                             CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) AS table_name,
                            COLUMN_NAME AS column_name 
                            FROM INFORMATION_SCHEMA.COLUMNS
                            WHERE COLUMN_NAME IN ('YOUR_COLUMN_NAME')
                                AND TABLE_SCHEMA='YOUR_TABLE';
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

  OPEN cur1;

  read_loop: LOOP
    FETCH cur1 INTO _tablename;

    IF done THEN
      LEAVE read_loop;
    END IF;

    SET @s = CONCAT('SELECT @student_number := COUNT(DISTINCT ', _columnname,') ',_tablename,' FROM ', _tablename, ' WHERE ', _columnname,' = \'', student_name,'\'');
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

    SET @s = CONCAT('SELECT @ total_student_number := COUNT(DISTINCT ', _columnname,') ',_tablename,' FROM ', _tablename);
    PREPARE stmt FROM @s;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

    INSERT INTO getStudentResults VALUES (_tablename,@student_number,@total_student_number);

  END LOOP;

  CLOSE cur1;

END$$

将getStudentResults声明为

    CREATE TEMPORARY TABLE IF NOT EXISTS getStudentResults (
   classroom_name VARCHAR(255),
   student_number INT DEFAULT 0,
   total_student_number INT DEFAULT 0
 );