简体   繁体   English

PHP,SQL多表

[英]PHP,SQL multiple Tables

im in need of help. 我需要帮助。 I created a search for my website. 我为我的网站创建了一个搜索。 Now i have created a second table in the database with older data. 现在,我在数据库中用旧数据创建了第二个表。 I want to retreive that data on the same search but I can't find the right way to do it. 我想在相同的搜索中检索该数据,但找不到正确的方法。 I have read some tutorials and tried stuff but I just cant figure it out how to do it properly. 我已经阅读了一些教程并尝试了一些东西,但是我无法弄清楚如何正确地做到这一点。

    $raw_results = mysql_query("SELECT BSTKATNR, BSTLIEFBST, BSTARTBES1, BSTANF, BSTSTLIO, BSTLIEFMIN, BSTMIND, BSTARTMASS, BSTKUMST, BSTKUMVK, BSTKUMER  FROM elebest
    WHERE 
    ( `BSTLIEFBST` LIKE '%".$query."%' )
    OR 
    ( `BSTKATNR` LIKE '%".$query."%' )
    OR 
    ( `BSTLIEFTXT` LIKE '%".$query."%' )
    OR 
    ( `BSTARTBES1` LIKE '%".$query."%') ") or die(mysql_error());

This is the code that works and searches everything I need. 这是可以工作并搜索我需要的所有内容的代码。 Is it somehow possible to select 3 columns from the table "olddata" ? 是否可以从表“ olddata”中选择3列? I hope someone can help me. 我希望有一个人可以帮助我。 The table "olddata" only has 3 columns so I don't think that JOIN would work... 表“ olddata”只有3列,所以我认为JOIN不会起作用...

Thank you. 谢谢。

It sounds to me you should run two seperate searches, one for new data, one for old data. 在我看来,您应该运行两个单独的搜索,一个搜索新数据,一个搜索旧数据。 If you have seperate tables with seperate data from seperate timestamps. 如果您有带有来自不同时间戳记的单独数据的单独表。 You can't use a JOIN if there are no overlapping fields. 如果没有重叠的字段,则不能使用JOIN。

Here's a PDO snippet you can use for safer queries: 这是您可以用于更安全查询的PDO代码段:

$dbHost = 'databaseHost';
$dbName = 'databaseName';
$dbUser = 'databaseUsername';
$dbPass = 'databasePassword';

/**
 * Connect to your database.
 */
try
{
    $dsn = 'mysql:dbname=%s;host=%s';
    $conn = new PDO(vsprintf($dsn, [$dbName, $dbHost]), $dbUser, $dbPass);
} catch (PDOException $e)
{
    /**
     * Catch any exceptions in case your connection should fail.
     */
    echo 'Failed to connect: '.$e->getMessage();
    die();
}
/**
 * Build your queries.
 */
$firstQuery = 'SELECT 
    t1.`BSTKATNR`,
    t1.`BSTLIEFBST`,
    t1.`BSTARTBES1`,
    t1.`BSTANF`,
    t1.`BSTSTLIO`,
    t1.`BSTLIEFMIN`,
    t1.`BSTMIND`,
    t1.`BSTARTMASS`,
    t1.`BSTKUMST`,
    t1.`BSTKUMVK`,
    t1.`BSTKUMER`
    FROM `table1` AS t1 WHERE 
    ( t1.`BSTLIEFBST` LIKE :BSTLIEFBST ) OR
    ( t1.`BSTKATNR` LIKE :BSTKATNR ) OR
    ( t1.`BSTLIEFTXT` LIKE :BSTLIEFTXT ) OR
    ( t1.`BSTARTBES1` LIKE :BSTARTBES1);'
;
$secondQuery = 'SELECT 
    t2.`BSTKUMSTVJ`,
    t2.`BSTKUMVKVJ`,
    t2.`BSTKUMERVJ`
    FROM `vorjahr` AS t2 WHERE 
    ( t2.`BSTKUMSTVJ` LIKE :BSTKUMSTVJ) OR
    ( t2.`BSTKUMVKVJ` LIKE :BSTKUMVKVJ) OR
    ( t2.`BSTKUMERVJ` LIKE :BSTKUMERVJ);'
;
/**
 * Prepare your statements
 */
$stmtOne = $conn->prepare($firstQuery);
$stmtTwo = $conn->prepare($secondQuery);
/**
 * Bind your query to all values.
 */
$stmtOne->bindValue(':BSTLIEFBST', '%'.$query.'%', PDO::PARAM_STR);
$stmtOne->bindValue(':BSTKATNR', '%'.$query.'%', PDO::PARAM_STR);
$stmtOne->bindValue(':BSTLIEFTXT', '%'.$query.'%', PDO::PARAM_STR);
$stmtOne->bindValue(':BSTARTBES1', '%'.$query.'%', PDO::PARAM_STR);

$stmtTwo->bindValue(':BSTKUMSTVJ', '%'.$query.'%', PDO::PARAM_STR);
$stmtTwo->bindValue(':BSTKUMVKVJ', '%'.$query.'%', PDO::PARAM_STR);
$stmtTwo->bindValue(':BSTKUMERVJ', '%'.$query.'%', PDO::PARAM_STR);
/**
 * Execute the statement
 */
$stmtOne->execute();
$stmtTwo->execute();
/**
 * Return your resultset
 */
$resultset1 = $stmtOne->fetchAll();
$resultset2 = $stmtTwo->fetchAll();

Just create two of these and combine your results! 只需创建其中两个并将您的结果合并即可!

You can use joins for retrieving multiple data from different tables: 您可以使用联接从不同的表中检索多个数据:

select 
    table1.id, table1.name, table2.id, table2.name 
from 
    table1 
left join 
    table2 on (table1.id = table2.id) 
where 
    (table1.`BSTLIEFBST` LIKE '%".$query."%')
    or (table1.`BSTKATNR` LIKE '%".$query."%')
    or (table2.`BSTLIEFTXT` LIKE '%".$query."%')
    or (tabl2.`BSTARTBES1` LIKE '%".$query."%')

I hope it will help you. 希望对您有帮助。 If you have still confusion the please commits your tables with field and specify which columns you need I will create query for you 如果您仍然感到困惑,请用字段提交表并指定您需要的列,我将为您创建查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM