MYSQL select query based on another tables entries

前端 未结 2 928
情深已故
情深已故 2020-12-19 06:13

I have stumped on this as I am a total beginner in MySql.

Here is a the basic of how the two tables are formed

Table 1 id,product_id, product_name

相关标签:
2条回答
  • 2020-12-19 07:05

    It's pretty simple to join two tables:

    select t1.* 
    from Table1 t1
    join Table2 t2 on t1.product_id = t2.product_id
    where t2.active = 'Y'
    
    0 讨论(0)
  • 2020-12-19 07:12

    You could use JOIN (as Fosco pointed out), but you can do the same thing in the WHERE clause. I've noticed that it's a bit more intuitive method than JOIN especially for someone who's learning SQL. This query joins the two tables according to product_id and returns those products that are active. I'm assuming "active" is boolean type.

    SELECT t1.*
    FROM Table1 t1, Table2 t2
    WHERE t1.product_id = t2.product_id AND t2.active = TRUE
    

    W3Schools has a good basic level tutorial of different kinds of JOINs. See INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN.

    0 讨论(0)
提交回复
热议问题