dongqiao9015 2013-05-27 10:01
浏览 94
已采纳

在第一个表上使用WHERE子句进行LEFT OUTER JOIN

I want to SELECT a some rows from a table, but these rows must satisfy a condition like column = value. Here's my SELECT statement:

$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.

  • 写回答

1条回答 默认 最新

  • doucan4815 2013-05-27 10:06
    关注

    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. Also i put the category selection in the where clause since imho this makes it more readable.

    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. You can read more about SQL-Injections here:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序