0

我不是 sql 编程方面的专家,我想知道您是否可以帮助我使用 INNER JOIN 重新编写以下查询?

db_query("
    select max(field_date_and_time_value2) as last_time 
    from field_data_field_date_and_time 
    where (field_date_and_time_value2 > '".$today."') 
    AND (".$node->uid." = (select uid from node where nid = " . $node->nid ."))");
4

2 回答 2

0

假设您的field_date_and_time表中有一个名为与表中该列uid具有相同含义的列node,这就是您需要做的。

      SELECT MAX(A.field_date_and_time_value2) AS last_time
        FROM field_date_and_time A
  INNER JOIN node B ON A.uid = B.uid
       WHERE B.nid = '".$node->nid."'
         AND A.field_date_and_time_value2 > '".$today."'

当心 SQL 注入攻击!避免将变量直接替换到 SQL 语句中,除非您确实确定这些变量不会被 badguy 用户破坏!记住那个叫的人的故事

"johnny ; drop tables *;" 
于 2013-07-15T14:49:09.503 回答
0

像这样的东西会起作用吗?

SELECT
    max(a.field_date_and_time_value2) as last_time
    , b.uid
FROM field_data_field_date_and_time a
    INNER JOIN
        node b
    ON
        /* this piece might be wrong. what is the relationship between the first table and the second one? */
        b.nid = '".$node->nid."'
where
    a.field_date_and_time_value2 > '".$today."' AND
    b.uid = $node->nid
于 2013-07-15T14:12:28.340 回答