Select multiple tables with WHERE - php

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';

Related

Php how to use multi tables in one mysql query

So I have two tables in my phpmyadmin like
tabel1 and tabel2
in both tables, i want to select id = 2
so I have tried
mysql_query('SELECT * FROM tabel1, table2
WHERE id=2');
but not working plz give me some suggestions
You can use UNION ALL to accomplish what you want.
SELECT * FROM table1 WHERE id = 1
UNION ALL
SELECT * FROM table2 WHERE id = 1
Depending on your exact requirements, the query might be as easy as
SELECT *
FROM table1, table2
WHERE table1.id=2 AND table2.id=2
You are implicitly joining your tables for the condition of table1.id and table2.id being equal 2.
Use inner join and also read Manual
mysql_query('SELECT * FROM tabel1 as t1
inner join table2 as t2 on t1.id=t2.id
WHERE t1.id=2');
I think you have a typo there. You said you have table1and table2
Then your SQL statement should be
mysql_query('SELECT * FROM table1, table2 WHERE id=2');
instead of
mysql_query('SELECT * FROM tabel1, table2 WHERE id=2');
what was in your question.

Sql statement to join two table

Suppose I have two table
Table Name group
column 1: giver
column 2: acceptor
Table name userinfo
column 1: name
column 2: status
i want to select giver,acceptor and userinfo.status that from group table where giver or acceptor whose name is zakir that giver or acceptor exist in uerinfo table as name.
Need Help to write sql statement for taht query..
Thanks in advance... :)
What you are referring to is also known as the INNER JOIN clause in an SQL Statement.
Depending on the relationship you can create an INNER JOIN to potentially connect the two variables that are identical.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Extracted from W3Schools.com
Try the below query,
SELECT
gp.giver,gp.acceptor,ui.status
FROM group as gp JOIN userinfo as ui
on ui.name = 'zakir'
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')
Or try this without join,
SELECT
gp.giver,gp.acceptor,ui.status
FROM group as gp, userinfo as ui
WHERE ui.name = 'zakir'
AND (gp.giver = 'zakir' or gp.acceptor = 'zakir')
Try this:
SELECT `group`.`giver`, `group`.`acceptor`, `userinfo`.`status`
FROM `group`, `userinfo`
WHERE (`group`.`giver` = 'zakir' OR `group`.`acceptor` = 'zakir')
AND `userinfo`.`name` = 'zakir'
select g.giver, g.acceptor u.status
from group g, userinfo u
where u.name = 'zakir'
and (g.giver = u.name or g.acceptor = u.name)
It should do a part of the job.
This assumes you want the status for both the giver and the acceptor
May need a little tweaking for mysql syntax
Select giver, g.status, acceptor, a.status
FROM GROUP
join userinfo as g on group.giver = g.name
join userinfo as a on group.acceptor = a.name
where (giver = 'zakir' or acceptor = 'zakir')

select multiple tables mysql

I am trying to select two tables with where clause,
The problem: I am getting more than 2 result. something like 123451111 I only have two ids with the value 1. I think I am doing it wrong.
The tables don't have the same structure and are not related by any means. Any ideas?
<?php include_once("config.php");
$s = '1';
$stmt =$mydb->prepare("select * FROM table1,table2 where table1.id = ? or table2.id = ?");
stmt->bind_param('ss', $s, $s);
echo $mydb->error;
$stmt->execute();
?>
<?php
$results = $stmt->get_result();
while ($row = $results->fetch_assoc()) {
echo $row['id']."<br/>";
}
?>
You need to have a join between table1 and table2 on some unique column, say id.
select * FROM table1,table2 where table1.id = table2.id;
Additionally you can have multiple filter conditions( say you want to filter the tables on id=101 -
select *
FROM table1,table2
where table1.id = table2.id
and table1.id = 101;
Hope this helps. Whenever you have multiple tables in a SQL statement, you need to join them otherwise the engine would make cartesian product as it happens in Cartesian product of mathematical set theory.
Basically you should have at least n-1 join conditions where n is the number of tables used.
Your question is a little problematic, but if your problem is not getting two id's, but you are getting one correctly with the use of a JOIN, you may be looking for a IN clause:
SELECT *
FROM table1,table2
WHERE table1.id = table2.id
AND table1.id IN (ID1, ID2);
Using IN instead of = lets you match multiple values to the table.id. This way, you get data from both tables and you get both ID's
This is join usage :
select t1.*,t2.* FROM table1 t1
left join table2 t2
on t1.id = t2.id
where t1.id = "keyword"

Search in two mysql tables with non identical tables (keyword search)

I want to make my keyword search in two mysql tables. my tables don't have any identical column names. But I tried few queries, they didn't work for me.
Keyword IS 07731A0328
I tried this:
$sql = "select a.*, b.* from table1 a inner join table2 b on a.col1=b.htno WHERE a.col1 like '$name'";
$sql = "select a.*, b.* from table1 a join table2 b on a.col1=b.htno WHERE a.col1 like $name";
Can someone help me with this? Thank you!
TABLE 1
TABLE2
Join is your friend:
http://www.w3schools.com/sql/sql_join.asp
Combine rows from two or more tables, based on a common field between them.
SELECT * FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.col1=TABLE2.htnon
WHERE TABLE1.col1 = "07731A0328"
The query will be
SELECT * FROM Table1,Table2
WHERE Table1.col1=Table2.htnon AND Table1.col1 = "07731A0328"

Help with grasping (INNER?) JOIN

I'm having trouble building a query. I can do what I want in 3 different queries.
SELECT id FROM table1 WHERE url LIKE '%/$downloadfile'
put that in $url_id
SELECT item_id FROM table2 WHERE rel_id = '$url_id'"
put that in $item_id
SELECT rel_id FROM table2 WHERE rel_id = '$item_id' AND field_id = '42'"
put that in $user_id
But from reading examples on joins and inner joins I think there's a more elegant way. I cant wrap my brain around writing a better query (but would like to) I can describe how it should go:
table1
fields: id, url
table2
fields item_id, rel_id, field_id
I know the last part of table1.url (LIKE '%/$filename') with that I select table1.id.
table1.id is equal to one entry in table2.rel_id. So get that and select the table2.item_id.
In table2 there is another entry which has the same table2.item_id and it will have a table2.field_id = '42'
And finally the value I need is the table2.rel_id where the table2.field_id was 42.
I will fetch that value and put it in $user_id
Can this be done with one query using joins/inner joins?
SELECT url, second.rel_id AS user_id
FROM table1
INNER JOIN table2 AS first
ON table1.id=first.rel_id
INNER JOIN table2 AS second
ON first.item_id=second.rel_id
WHERE second.field_id='42'
AND table1.url LIKE '%/:downloadfile'
Sure, should be something like:
SELECT t2_second.rel_id
FROM table1 t1
INNER JOIN table2 t2_first ON t1.id = t2_first.rel_id
INNER JOIN table2 t2_second ON t2_second.rel_id = t1_first.item_it
WHERE
t1.url = '%/:filename' AND
t2_second.field_id = 42
You could even throw in a distinct so that each t2_second.rel_id is only returned once:
SELECT DISTINCT [...]
You might not need this, however, if t2_second.field_id = 42 will restrict the query to only return one row for each t2_second.rel_id
This the query that works for me, made one small change to Ignacio's answer:
SELECT url, second.rel_id AS user_id
FROM table1
INNER JOIN table2 AS first
ON table1.id=first.rel_id
INNER JOIN table2 AS second
ON first.item_id=second.item_id
WHERE second.field_id='42'
AND table1.url LIKE '%/:downloadfile'

Categories