sql select from table then compare with other table in one statement

Name

I have to tables in one database.

  1. users
  2. user_activate

I have to variables in php

  1. $username = foo;
  2. $key = jas823k123ksd34324;

Now I want to select from the table users the attribute user_id where user_username == $username

I do this with this statement

$sql = "SELECT user_id FROM users WHERE user_username = '$username'";
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_assoc($result)){
    $user_id = $row['user_id'];
}

Now I want to select from the table user_activate the attribute user_activate_key where user_activate_key == $key;

For this I use this statement:

$sql2 = "SELECT user_activate_key FROM user_activate WHERE user_activate_key = '$key'";
$result2 = mysqli_query($db, $sql2);
while($row = mysqli_fetch_assoc($result)){
    $user_key = row['user_activate_key'];
}

Can I do both statements in one statement?

TobyLL

As you've written it, two seperate queries is the correct way to do it. But I suspect that there's some kind of relationship between users and user_activate that might make what you're asking for make sense. Assuming that a user_activate_key is tied to a specific user_id, you could do something like the following:

select users.user_id, ua.user_activate_key
from users u
left join user_activate ua
    on u.user_id = ua.user_id
    and ua.user_activate_key = '$key'
where u.username = '$username'

The LEFT JOIN means that the user will be shown even if there isn't a matching user_activate_key record.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL : Select statement order by other table

From Dev

SQL statement (for loop) with SUM from other table

From Dev

SQL - Compare data from one table

From Java

SQL query to select from one table based on a column value in other table

From Dev

SQL - Pivot table from a multilayer SELECT statement

From Dev

SELECT * from SQL table using prepared statement

From Dev

MYSQL query SELECT from one table and join results with other table

From Dev

select data from one table depending smilarity of other table

From Dev

PHP SQL - Multiple select from one table

From Dev

PostgreSQL: How to add Column from other table in select statement?

From Dev

MySQL / PDO - Select from other table (Join statement)

From Dev

get data from select statement with table name as a result of other query

From Dev

SQL statement for join but not in other table

From Dev

Compare 2 tables and select data from one table

From Dev

select from one table, insert into another table oracle sql query

From Dev

SQL query to select row from one table which is not in another table

From Dev

while creating a table select one column from another table sql

From Dev

Multiple Table SQL SELECT statement

From Dev

SQL statement to return data from a table in an other sight

From Dev

mysql: Select all rows and compare with other table

From Dev

Select values from one table based on two other tables (relational)

From Dev

Select column from one table according to frequency of other?

From Dev

how to select one row from one table and multiple rows from other table using joins in mysql,

From Dev

Select statement from multi table

From Dev

MySQL-Need to select a row from one table to another table WHERE a <# will generate in SELECT statement

From Dev

SQL - How to select discontinuous rows from a table with one select?

From Dev

How to write an SQL statement for two variables, one carrying the table name, and the other the specific column in a table?

From Dev

SQL statement to select join multiple fields from different table?

From Dev

SQL join one table to other table twice

Related Related

  1. 1

    SQL : Select statement order by other table

  2. 2

    SQL statement (for loop) with SUM from other table

  3. 3

    SQL - Compare data from one table

  4. 4

    SQL query to select from one table based on a column value in other table

  5. 5

    SQL - Pivot table from a multilayer SELECT statement

  6. 6

    SELECT * from SQL table using prepared statement

  7. 7

    MYSQL query SELECT from one table and join results with other table

  8. 8

    select data from one table depending smilarity of other table

  9. 9

    PHP SQL - Multiple select from one table

  10. 10

    PostgreSQL: How to add Column from other table in select statement?

  11. 11

    MySQL / PDO - Select from other table (Join statement)

  12. 12

    get data from select statement with table name as a result of other query

  13. 13

    SQL statement for join but not in other table

  14. 14

    Compare 2 tables and select data from one table

  15. 15

    select from one table, insert into another table oracle sql query

  16. 16

    SQL query to select row from one table which is not in another table

  17. 17

    while creating a table select one column from another table sql

  18. 18

    Multiple Table SQL SELECT statement

  19. 19

    SQL statement to return data from a table in an other sight

  20. 20

    mysql: Select all rows and compare with other table

  21. 21

    Select values from one table based on two other tables (relational)

  22. 22

    Select column from one table according to frequency of other?

  23. 23

    how to select one row from one table and multiple rows from other table using joins in mysql,

  24. 24

    Select statement from multi table

  25. 25

    MySQL-Need to select a row from one table to another table WHERE a <# will generate in SELECT statement

  26. 26

    SQL - How to select discontinuous rows from a table with one select?

  27. 27

    How to write an SQL statement for two variables, one carrying the table name, and the other the specific column in a table?

  28. 28

    SQL statement to select join multiple fields from different table?

  29. 29

    SQL join one table to other table twice

HotTag

Archive