sql select查询来自两个不同的表WHERE active = 1

时间:2016-04-01 14:14:07

标签: mysql database

我尝试从两个不同的表中选择所有列WHERE active = 1

我有2个表 table_pro table_basic

  

sql:"select * from table_basic,table.name";

     

和2条件:

     
      
  1. WHERE active = 1
  2.   
  3. WHERE table_pro.id = table_basic.name.id
  4.   

如何正确使用

这是table_pro

+----+--------+---------+-----------+
| id | people | rooms   |  active   | 
+----+--------+---------+-----------+
|  1 |      5 |      10 | 0         |
|  2 |     12 |      17 | 0         |
|  3 |     21 |      38 | 1         |
+----+--------+---------+-----------+

这是table_basic

+---------+-------+---------+------------+----------+
| name_id | name  | balance | title      |  time    |
+---------+-------+---------+------------+----------+
|  1      |shop   | 100     | failed     | 15:10:20 |
|  2      |factory| 75      | error      | 15:10:20 |
|  3      |studio | 25      | timed_out  | 15:10:20 |
+---------+-------+---------+------------+----------+

我希望此输出结果只有行(来自所有列),状态为active = 1

+-----+-------+----- --+--------+-------+----------+---------+--------+
| id  | people| rooms  | name   |balance| title    | time    | active |           
+-----+-------+--------+--------+-------+----------+---------+--------+
|  3  | 21    | 38     | studio |25     | timed_out| 15:10:20|    1   |
+-----+-------+--------+--------+-------+----------+---------+--------+

由于

2 个答案:

答案 0 :(得分:1)

SELECT A.id, A.people, A.rooms, B.name, B.balance, B.title, B.time, A.active 
FROM 
    table_pro AS A
JOIN 
    table_basic AS B
ON 
    A.id = B.name_id
WHERE 
    A.id = 3

答案 1 :(得分:0)

SELECT table_pro.*, table_basic.*
FROM table_pro
INNER JOIN table_basic
    ON table_basic.name_id = table_pro.id
WHERE table_pro.active = 1