-4

I have two tables with this structure

Table 1
E-mail - User - Login
Table 2
Deals - Dealer - Login

I would like to do something similar to Select from table 1 the email get the login and echo table 2 deals & dealer

Now i done this but I know is wrong

$sql = "SELECT Login,Deals,Dealers FROM users AND deals where Email = '$email' ";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo " <strong> Balance: </strong> " . $row["Balance"]. " <strong>MT5 Trading Number</strong> " . $row["Login"].  "<br>";
    }
} else {
    echo "0 results";

}
Elpidios
  • 29
  • 6
  • Welcome to stack overflow try to show some code of how you attempted to solve the problem – Bobby Axe Oct 16 '18 at 12:43
  • Please see: [Why should I provide an MCVE for what seems to me to be a very simple SQL query?](https://meta.stackoverflow.com/q/333952/2469308) – Madhur Bhaiya Oct 16 '18 at 12:45

2 Answers2

0

You can do this :

SELECT t1.Email, t1.Login, t2.Deals, t2.Dealer
FROM users as t1
LEFT JOIN deals as t2 ON t2.Login = t1.Login
WHERE t1.Email = :email
  1. I select the column of each table using an alias (t1 and t2)
  2. I select those row from table users then use a join based on the column Login
  3. I added a where condition on Email as you did it too

Try to use prepare statement (:email) and bind the param $email to avoid SQL injection


EDIT :

Now if you want only the Email and Login of users that have some Deals and Dealers in table deals : replace the LEFT JOIN by INNER JOIN


Here is some link to understand how MySQL join works :

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
0

Try below :

select Table2.Login, Deals, Dealer from Table1 inner join Table2 on Table1.Login = Table2.login
where Table1.Email = 'something
FatemehNB
  • 271
  • 2
  • 7