在sql子查询中选择列的值

时间:2018-07-10 09:45:40

标签: mysql sql

我想显示数据库中所有表的实际header_titles(每个表中的一行)

我当前的查询

select column_name, table_name from information_schema.columns 
where column_name in (
select column_name from information_schema.tables
where table_schema='site'
) and 
column_name='header_title';

此刻查询返回“ header_titles” 12次(数据库中带有该字段的表的数量,显然还有表名。我不确定如何获取每个header_titles的值

1 个答案:

答案 0 :(得分:0)

一个非常简单的例子

更新-列值现在正在屏幕上打印

$query = "SELECT table_name FROM information_schema.tables AS tbl
WHERE EXISTS(SELECT 1 FROM information_schema.columns AS col 
WHERE tbl.table_name = col.table_name AND column_name = 'header_title')";

$result = mysqli_query($conn, $query) or trigger_error(mysqli_error($conn), E_USER_ERROR);
while($row = mysqli_fetch_row($result))
{
  $q = 'SELECT header_title FROM '.$row[0];
  $r = mysqli_query($conn, $q) or trigger_error(mysqli_error($conn), E_USER_ERROR);
  $index = 0;
  while($data = mysqli_fetch_row($r))
  {
    $index++;
    echo "Table = ".$row[0].", row = ".$index.", HEADER_TITLE = ".$data[0].PHP_EOL;
  }
}