Exelion
Exelion

Reputation: 27

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" ? I hope someone can help me. The table "olddata" only has 3 columns so I don't think that JOIN would work...

Thank you.

Upvotes: 1

Views: 72

Answers (2)

Dani
Dani

Reputation: 925

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

Upvotes: -1

Fyntasia
Fyntasia

Reputation: 1143

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.

Here's a PDO snippet you can use for safer queries:

$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!

Upvotes: 3

Related Questions