3

我正在尝试使用 MEDOO 连接两个表,我面临的问题是我正在构建的查询没有返回任何数据(它返回 false)。我已经按照这个普通的 SQL 代码完成了查询

select posts.id, title, extract, img_principal, users.username, date, category, platform, url from posts, users where users.username in (select username from users where users.id = posts.author);

结果是这个:

    $lastPost = 10;

    $database = new medoo([
        'database_type' => 'mysql',
        'database_name' => 'notijuegosdb',
        'server' => 'localhost',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8'
    ]);


    $database->select("posts", [

        "[>]users" => ["author" => "users.id"]
    ], [
      'posts.id',
      'posts.title',
      'posts.extract',
      'posts.img_principal',
      'users.username',
      'posts.date',
      'posts.category',
      'posts.platform',
      'posts.url'
    ], [
        'posts.id[<]'=> $lastPost
    ]);

当我将 SQL 代码直接用于数据库时,它可以完美运行,因此这可能是我构建查询的方式的问题......不知道可能出了什么问题。

提前非常感谢。

4

1 回答 1

1

您可以使用用于复杂查询的查询 medoo 方法将其存档:

$postsData = $database->query("select posts.id, title, extract, img_principal, users.username, date, category, platform, url from posts, users where users.username in (select username from users where users.id = posts.author);")->fetchAll();

就像 Medoo doc 说的:

此功能用于复杂查询的特殊和自定义 SQL 查询。对于将要插入的每个数据,请使用引号功能来防止 SQL 注入。

Medoo API

于 2016-05-19T15:25:40.540 回答