dongwen1871 2015-09-30 16:08
浏览 19
已采纳

如何在PDO中编写此查询? [关闭]

I had used a query using mysql_query() and mysql_connect(), bun now i am using PDO in php

please help me, what is the best and fastest way for this query:

my old query was:

$sql = "SELECT * FROM products WHERE publish = '1' 
    AND id IN 
        (SELECT product_id FROM category_controller WHERE category_id IN 
                (SELECT id FROM categories WHERE publish = '1'))";
$result = mysql_query($sql);

my new query is:

$PDO = new PDO("mysql:host=localhost;dbname=mydb", 'root', '');
$sql = "SELECT * FROM products WHERE publish = '1' 
    AND id IN 
        (SELECT product_id FROM category_controller WHERE category_id IN 
                (SELECT id FROM categories WHERE publish = '1'))";

$result = $PDO->query($sql);

but the both ways that i had used was very slowly beacuse there are more than 5000 records in products table.

please help me to find a new and fastest way to run this query.

  • 写回答

2条回答 默认 最新

  • doucheng1884 2015-09-30 16:24
    关注

    You may speed this up by using the JOIN concept rather than all the sub queries like this.

    SELECT p.* 
    FROM products p
       JOIN category_controller cc ON cc.category_id = c.id
       JOIN categories c ON c.publish = 1
    WHERE p.publish = 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作