简体   繁体   English

在第一个表上具有WHERE子句的LEFT OUTER JOIN

[英]LEFT OUTER JOIN with a WHERE clause on the first table

I want to SELECT a some rows from a table, but these rows must satisfy a condition like column = value . 我想从表中SELECT一些行,但是这些行必须满足诸如column = value的条件。 Here's my SELECT statement: 这是我的SELECT语句:

$query = "SELECT t1.title, t1.introtext, t2.jr_street, t2.jr_city, 
    t2.jr_state, t2.jr_postalcode, t2.jr_country, t3.created, t3.name, 
    t3.title, t3.comments, t4.ave 
FROM $table1 t1
    LEFT OUTER JOIN $table2 t2 ON t1.id = t2.contentid
    LEFT OUTER JOIN $table3 t3 ON t1.id = t3.pid
    LEFT OUTER JOIN $table4 t4 ON t1.id = t4.reviewid
";

I tried adding a WHERE clause after the FROM statement, but I am getting an SQL syntax error. 我尝试在FROM语句后添加WHERE子句,但出现SQL语法错误。

Seems like I have to guess your problem. 好像我不得不猜测你的问题。 Next time you should give us your full example a the exact error. 下次您应该给我们提供完整示例的确切错误。 Did you try something like this? 你尝试过这样的事情吗?

$value = (int) $_POST['value'];
$query = "SELECT t1.title, t1.introtext, t2.jr_street, t2.jr_city, t2.jr_state,
t2.jr_postalcode, t2.jr_country, t3.created, t3.name, t3.title, 
t3.comments, t4.ave FROM $table1 t1
LEFT OUTER JOIN $table2 t2 ON t1.id = t2.contentid
LEFT OUTER JOIN $table3 t3 ON t1.id = t3.pid
LEFT OUTER JOIN $table4 t4 ON t1.id = t4.reviewid
WHERE t2.column = ".$value."
AND t1.catid=8";

I just guessed you want to select from table2 ;) You have to fill in the right table and column. 我只是猜测您要从table2中选择;)您必须填写正确的表和列。 Also i put the category selection in the where clause since imho this makes it more readable. 我也将类别选择放在where子句中,因为恕我直言,这使它更具可读性。

Please make absolutely sure that all variables like $value (and the tables) are save for usage in a query, for example that $value is an integer and the tables are explicitly set in your code and are not a user input. 请绝对确保将所有变量(如$value (和表))保存下来以在查询中使用,例如, $value是整数,并且表已在您的代码中显式设置,而不是用户输入。 You can read more about SQL-Injections here: 您可以在此处阅读有关SQL注入的更多信息:

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

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