简体   繁体   中英

Select multiple tables with WHERE

Can somebody tell me how to select multiple tables with WHERE in the query?

 $sql = "SELECT * FROM leerlingen, leraren WHERE voornaam= '$username' AND password= '$password'"; 

Because this query gives an error

SELECT table1.*, 
       table2.* 
FROM table1 
  JOIN table2 
    ON table1.id = table2.table1_id 
WHERE table1.field LIKE "Hello" 
  AND table2.field LIKE "World;

This query should do what you are asking: Selecting everything from two different tables having different conditions in the WHERE part. As someone else said, try to always use explicit JOIN syntax as is more readable and self-explanatory.

Since the code you wrote does not make it clear if you did, I will strongly suggest you use some sort of input validation and escaping to prevent SQL Injection .

To use multiple tables in where clause you have to use join between those tables

   select le.*,l.* from leerlingen le join  leraren l on le.col=l.col
    where l.col1=value --table leraren in where
       and le.col1=value --table leerlingen in where

And then you can apply where filter on them

SELECT t1.column_names, t2.column_names FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.t1_id WHERE t1.voornam = $username AND t1.password = $password t2.field ='some value';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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