从具有多个表的查询中获取pdo表信息

时间:2015-06-18 02:22:38

标签: php mysql pdo

$lister_query = "
  SELECT * FROM (SELECT 
    lister1_password lister_password,
    lister1_status   lister_status,
    lister1_email    lister_email
  FROM lister1
  UNION
  SELECT 
    lister2_password lister_password,
    lister2_status   lister_status,
    lister2_email    lister_email
  FROM lister2
  UNION
  SELECT 
    lister3_password lister_password,
    lister3_status   lister_status,
    lister3_email    lister_email
  FROM lister3) AS lister
  WHERE lister.lister_password = ? AND lister.lister_email = ?
"; 

$lister_prepare = $database_listers -> prepare($lister_query);
$lister_prepare                     -> execute($_EXECUTE);

$_LISTER_COUNT = $lister_prepare -> rowCount();

if ($_LISTER_COUNT === 1) {
  $_LISTER = $lister_prepare -> fetch();

  $lister_status = $_LISTER['lister_status'];
  $lister_key    = $_LISTER['lister_key'];
}

如何找到电子邮件和电子邮件的表格?找到密码集。我需要知道是否找到了lister1,lister2或lister3。这可能吗?

解决方案(目前无效)

$lister_query = "
      SELECT * FROM (SELECT 
        lister1_password lister_password,
        lister1_status   lister_status,
        lister1_email    lister_email,
        'test1' test
      FROM lister1
      UNION
      SELECT
        lister2_password lister_password,
        lister2_status   lister_status,
        lister2_email    lister_email,
        'test2' test
      FROM lister2
      UNION
      SELECT
        lister3_password lister_password,
        lister3_status   lister_status,
        lister3_email    lister_email,
        'test3' test
      FROM lister3) AS lister
      WHERE lister.lister_password = ? AND lister.lister_email = ?
    "; 

    $lister_prepare = $database_listers -> prepare($lister_query);
    $lister_prepare                     -> execute($_EXECUTE);

    $_LISTER_COUNT = $lister_prepare -> rowCount();

    if ($_LISTER_COUNT === 1) {
      $_LISTER = $lister_prepare -> fetch();

     echo $_LISTER['test'];

      $lister_status = $_LISTER['lister_status'];
      $lister_key    = $_LISTER['lister_key'];
    }

我添加了解决的解决方案。它不会回显测试变量,而其他已获取变量的非现在正在运行。

2 个答案:

答案 0 :(得分:0)

试试这个,

$lister_query = "
  SELECT * FROM (SELECT 
    lister1_password lister_password,
    lister1_status   lister_status,
    lister1_email    lister_email,
    'lister1' as table_name
  FROM lister1
  UNION
  SELECT 
    lister2_password lister_password,
    lister2_status   lister_status,
    lister2_email    lister_email,
    'lister2' as table_name
  FROM lister2
  UNION
  SELECT 
    lister3_password lister_password,
    lister3_status   lister_status,
    lister3_email    lister_email,
    'lister3' as table_name
  FROM lister3) AS lister
  WHERE lister.lister_password = ? AND lister.lister_email = ?
"; 

答案 1 :(得分:0)

您可以为定义表格的每个UNION SELECT添加另一列

$lister_query = "
  SELECT * FROM (SELECT 
    lister1_password lister_password,
    lister1_status   lister_status,
    lister1_email    lister_email,
    'lister1'        lister_table  // define lister_table
  FROM lister1
  UNION
  SELECT 
    lister2_password lister_password,
    lister2_status   lister_status,
    lister2_email    lister_email,
    'lister2'        lister_table  // define lister_table
  FROM lister2
  UNION
  SELECT 
    lister3_password lister_password,
    lister3_status   lister_status,
    lister3_email    lister_email,
    'lister3'        lister_table  // define lister_table
  FROM lister3) AS lister
  WHERE lister.lister_password = ? AND lister.lister_email = ?
"; 

...
[rest of your code]
...

if ($_LISTER_COUNT === 1) {
  $_LISTER = $lister_prepare -> fetch();

  $lister_table = $_LISTER['lister_table']; // the table it came from
  $lister_status = $_LISTER['lister_status'];
  $lister_key    = $_LISTER['lister_key'];
}