如何重新格式化SQL查询 [英] How to reformulate an sql query

查看:211
本文介绍了如何重新格式化SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是sql编程专家,我想知道您是否可以使用INNER JOIN重新构造以下查询?

i am not an expert in sql programming and i was wondering if you can help me reformulate the following query using 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 ."))");

推荐答案

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

Assuming your field_date_and_time table has a column called uid having the same meaning as that column in your node table, here's what you need to do.

      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语句!记得那个叫小伙子的故事

Beware SQL injection attacks! Avoid substituting variables directly into SQL statements unless you're really sure those variables can't be corrupted by badguy users! Remember the story of the guy named

"johnny ; drop tables *;" 

这篇关于如何重新格式化SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆