MySQL - Selecting data from another table for a WHERE clause

user9375768

I have the following MySQL query (failed attempt):

SELECT * FROM 
(SELECT  @rownum:=@rownum+1 rank, id, userID, total 
 FROM table1 total, (SELECT @rownum:=0) r 
 WHERE id = 318467989655781389 && mydb.table2.colm = 'false' 
 ORDER BY total DESC) a 
LIMIT 10

However, I get an error saying that mydb.table2.colm isn't in the field list. How can I get data from another table to use in a WHERE clause?

Gordon Linoff

You would use a JOIN. The query would look something like this:

SELECT (@rownum := @rownum + 1) as rank, t1.id, t1.userID, t1.total 
FROM table1 t1 JOIN
     mydb.table2 t2
     ON t1.? = t2.? CROSS JOIN
     (SELECT @rownum := 0) params
WHERE t1.id = 318467989655781389 AND t1.colm = 'false' 
ORDER BY t1.total DESC
LIMIT 10;

The ? is for the columns used for joining the tables together.

Notes:

  • Use table aliases and qualified column names whenever a query contains more than one table.
  • You can only reference a table that has been specific in the FROM clause.
  • A subquery is not needed for the LIMIT. In some versions of MySQL, a subquery might be necessary for the ORDER BY, but the query would look different.
  • The boolean AND operator in SQL is AND. Although && works in MySQL, you might as well use the standard operator.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Selecting data from two mysql tables where there may be nonexistent data in second table

From Dev

How to select data from columns in only one table when checking two tables limited by where clause in MySQL

From Dev

MySQL WHERE clause on unnormalized table

From Dev

Selecting entries from a table based on another table

From Dev

Selecting data from one table based on the value of another

From Dev

Laravel 5 where clause from another table

From Dev

Selecting Data from Joomla SQL Table where any value is the variable

From Dev

mysql - select from a table with sum() and where clause

From Dev

Use WHERE clause on a column from another table

From Dev

MySQL - Insert table data from another table

From Dev

MySQL WHERE clause link to another table

From Dev

Oracle SQL Selecting data from one table that relates to another

From Dev

Selecting records from a table and then selecting a count of records from another table

From Dev

Selecting Data from another table SQL Select Query

From Dev

How to select data from columns in only one table when checking two tables limited by where clause in MySQL

From Dev

MySQL SELECT all from first table and join matched data from second table with specific where clause for second table filter

From Dev

Insert data from a table to another one in MySQL with where condition

From Dev

where clause based on a select statement from another table

From Dev

Selecting data from table using another table's data

From Dev

WHERE clause from another table using NOT IN

From Dev

Selecting rows from one table where value and order from another in MYSQL

From Dev

MySQL - Insert table data from another table

From Dev

Not selecting data from table

From Dev

Selecting data from another SQL table to add to string text

From Dev

query for selecting data where id is max and where clause in eloquent laravel

From Dev

Transferring data from one table to another using a where clause mysql

From Dev

Selecting data from table where sum of values in a column equal to the value in another column

From Dev

Using column data from another table for the where clause of two joined tables

From Dev

mysql selecting data from one table based on data from another table

Related Related

  1. 1

    Selecting data from two mysql tables where there may be nonexistent data in second table

  2. 2

    How to select data from columns in only one table when checking two tables limited by where clause in MySQL

  3. 3

    MySQL WHERE clause on unnormalized table

  4. 4

    Selecting entries from a table based on another table

  5. 5

    Selecting data from one table based on the value of another

  6. 6

    Laravel 5 where clause from another table

  7. 7

    Selecting Data from Joomla SQL Table where any value is the variable

  8. 8

    mysql - select from a table with sum() and where clause

  9. 9

    Use WHERE clause on a column from another table

  10. 10

    MySQL - Insert table data from another table

  11. 11

    MySQL WHERE clause link to another table

  12. 12

    Oracle SQL Selecting data from one table that relates to another

  13. 13

    Selecting records from a table and then selecting a count of records from another table

  14. 14

    Selecting Data from another table SQL Select Query

  15. 15

    How to select data from columns in only one table when checking two tables limited by where clause in MySQL

  16. 16

    MySQL SELECT all from first table and join matched data from second table with specific where clause for second table filter

  17. 17

    Insert data from a table to another one in MySQL with where condition

  18. 18

    where clause based on a select statement from another table

  19. 19

    Selecting data from table using another table's data

  20. 20

    WHERE clause from another table using NOT IN

  21. 21

    Selecting rows from one table where value and order from another in MYSQL

  22. 22

    MySQL - Insert table data from another table

  23. 23

    Not selecting data from table

  24. 24

    Selecting data from another SQL table to add to string text

  25. 25

    query for selecting data where id is max and where clause in eloquent laravel

  26. 26

    Transferring data from one table to another using a where clause mysql

  27. 27

    Selecting data from table where sum of values in a column equal to the value in another column

  28. 28

    Using column data from another table for the where clause of two joined tables

  29. 29

    mysql selecting data from one table based on data from another table

HotTag

Archive