简体   繁体   English

PHP MySQL加入

[英]PHP MySQL Joins

Is there a way I could somehow do the following? 有什么办法可以执行以下操作吗?

Table ONE 表一

id   c_id   type
-------------------
1    1       7
2    2       7
3    3       5
4    4       7

Table TWO 表二

id   title   live
-------------------
1    row1    1
2    row2    0
3    row3    0
4    row4    1

The c_id column links the data from table ONE to table TWO. c_id列将数据从表ONE链接到表2。 Example: in table ONE, if the c_id is 2, that row in table ONE will be directly linked to table TWO's row with id 2, which has a title of "row2". 示例:在表ONE中,如果c_id为2,则表ONE中的行将直接链接到表2的ID为2的行,其标题为“ row2”。

I want to select from table ONE, everything with type 7, but only if their associated data in table TWO has live set to 1. 我想从表ONE中选择类型7的所有内容,但前提是它们在表TWO中的关联数据已实时设置为1。

Here's how I thought I'd do it, but this doesn't seem to work: 这是我认为自己会做的事情,但这似乎不起作用:

SELECT * FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' ORDER BY ONE.id DESC LIMIT 5

I would expect the above to return only rows 1 and 4 from table ONE, because although table ONE has three rows with type "7", only rows 1 and 2's associated row in table TWO have live set to 1. 我希望上面的代码仅返回表ONE的第1行和第4行,因为尽管表ONE包含三行类型为“ 7”的行,但只有表2的行1和2的关联行已设置为1。

select * from one join two on c_id = two.id where type=7 and live = 1
order by one.id desc limit 5

您接近了...尝试使用隐式联接:

SELECT ONE.* FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' AND ONE.c_id = TWO.id ORDER BY ONE.id DESC LIMIT 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM