Sql query selecting from both tables.

Yervand Khalapyan

I have a query which looks like this
$db->query("SELECT A.page_id, A.page_name FROM user_likes_pages as A , user_likes as B WHERE A.page_id = B.page_id AND B.country_id = ".$user_reg." ");
The thing is, I want to select a column from user_likes. Do I have to make a join or I can do it in different way. Thank you.

Gordon Linoff

You have a join in your query, but it is implicit. You should write the query as:

SELECT ulp.page_id, ulp.page_name, ul.<whatever>
FROM user_likes ul JOIN
     user_likes_pages ulp
     ON ul.page_id = ulp.page_id 
WHERE ul.country_id = ".$user_reg."

In addition to adding the explicit join syntax, I also changed the table aliases so they are abbreviations of the table name. This makes it easier to read the query and avoid mistakes.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQL Server, selecting from 2 columns from different tables

分類Dev

sql selecting from two tables without inner join

分類Dev

SQL query: combining data from two tables

分類Dev

SQL query with 3 tables

分類Dev

How to perform this type of MySQL query - selecting data based on 3 tables?

分類Dev

SQL query, join two tables

分類Dev

Query returns no results when data does not exist in both tables

分類Dev

sql: selecting specific data concerning two different tables

分類Dev

Which query works in both SQL Server and Oracle?

分類Dev

SQL How do I get Tables from Microsoft SQL Server Query Files?

分類Dev

SQL Queries from multiple tables

分類Dev

How to match two different tables in sql query?

分類Dev

SQL query, two tables, multiple items

分類Dev

Oracle SQL hierarchical query with two tables

分類Dev

Create multiple view tables from a single query

分類Dev

SQLite query to get data from multiple tables

分類Dev

No results from SQL Query

分類Dev

No results from SQL Query

分類Dev

How to order 2 related tables in one query having order fields both of them

分類Dev

SQL - Selecting unique values from one column then filtering based on another

分類Dev

SQL Server insert EXCEPT without selecting from a table

分類Dev

Selecting data from another SQL table to add to string text

分類Dev

Gathering data from three separate tables, sql

分類Dev

SQL Select From 2 different tables

分類Dev

Old SQL? multiple tables after FROM

分類Dev

Substract query from another query in SQL Server

分類Dev

Unable to figure out a SQL query involving 4 tables in MySQL

分類Dev

SQL query evaluates COUNT(*) differently if tables are defined as MyISAM or InnoDB

分類Dev

SQL Query joining two tables similar to left outer join

Related 関連記事

  1. 1

    SQL Server, selecting from 2 columns from different tables

  2. 2

    sql selecting from two tables without inner join

  3. 3

    SQL query: combining data from two tables

  4. 4

    SQL query with 3 tables

  5. 5

    How to perform this type of MySQL query - selecting data based on 3 tables?

  6. 6

    SQL query, join two tables

  7. 7

    Query returns no results when data does not exist in both tables

  8. 8

    sql: selecting specific data concerning two different tables

  9. 9

    Which query works in both SQL Server and Oracle?

  10. 10

    SQL How do I get Tables from Microsoft SQL Server Query Files?

  11. 11

    SQL Queries from multiple tables

  12. 12

    How to match two different tables in sql query?

  13. 13

    SQL query, two tables, multiple items

  14. 14

    Oracle SQL hierarchical query with two tables

  15. 15

    Create multiple view tables from a single query

  16. 16

    SQLite query to get data from multiple tables

  17. 17

    No results from SQL Query

  18. 18

    No results from SQL Query

  19. 19

    How to order 2 related tables in one query having order fields both of them

  20. 20

    SQL - Selecting unique values from one column then filtering based on another

  21. 21

    SQL Server insert EXCEPT without selecting from a table

  22. 22

    Selecting data from another SQL table to add to string text

  23. 23

    Gathering data from three separate tables, sql

  24. 24

    SQL Select From 2 different tables

  25. 25

    Old SQL? multiple tables after FROM

  26. 26

    Substract query from another query in SQL Server

  27. 27

    Unable to figure out a SQL query involving 4 tables in MySQL

  28. 28

    SQL query evaluates COUNT(*) differently if tables are defined as MyISAM or InnoDB

  29. 29

    SQL Query joining two tables similar to left outer join

ホットタグ

アーカイブ