简体   繁体   English

SubSelect或SubQuery

[英]SubSelect or SubQuery

Below will successfully find the product a customer has submitted * http://inks-etc.com/Canon/canon_inkdesign.php?ID=114 The ID=114 came from the $_GET['ID'] and when into the SELECT to receive the products: Price, Product No., Description, Image. 以下将成功找到客户已提交的产品* http://inks-etc.com/Canon/canon_inkdesign.php?ID=114 ID = 114来自$ _GET ['ID']以及何时进入SELECT接收产品:价格,产品编号,描述,图像。 Now I have been trying to get about 4 (LIMIT 4) more similar products to capture of the left side of the page so if the customer chooses a LC41 Black, I can provide LC41 Cyan, LC41 Magenta, LC41 Yellow as well. 现在,我一直在尝试获取更多(限4种)类似产品来捕获页面的左侧,因此,如果客户选择LC41黑色,我也可以提供LC41青色,LC41洋红色,LC41黄色。 I am thinking the 'SELECT Sub query' would be helpful. 我认为“ SELECT子查询”会有所帮助。 Will you tell me what I must modify and even begin again to perform this task. 您能告诉我我必须修改什么,甚至再次开始执行此任务。

    $inksetc = htmlentities($_GET['ID']);

     $sql2 = "SELECT Id,   Product_Type,  Product_No ,  miniImageFarm

         FROM `Canon_INK`

         WHERE IN
             (SELECT Id= $inksetc FROM Canon_INK WHERE Id LIKE $inksetc) 

         LIMIT 4";

$result = mysqli_query($dbc,$sql2) or die(mysqli_error($dbc));

    while($rows = mysqli_fetch_array($result)){
?>

You don't need a sub-query for this and you need wildcard '%' so that you could search by pattern and your $sql2 should look like this therefore: 您不需要为此的子查询,而需要通配符'%'以便可以按模式搜索,因此$sql2应该如下所示:

$sql2 = "SELECT Id,   Product_Type,  Product_No ,  miniImageFarm ";
$sql2 .= "FROM `Canon_INK` ";
$sql2 .= "WHERE Id LIKE '%".SUBSTR($inksetc,0,4)."%'";

I added for % for matching search. 我为匹配搜索添加了%

I believe you want something like this: 我相信您想要这样的东西:

SELECT Id, Product_Type, Product_No, miniImageFar
FROM Canon_INK
WHERE SUBSTR(Id, 0, 4) = SUBSTR($inksetc, 0, 4)

Read up on the LIKE operator; 阅读LIKE运算子; it does not say one string is like another, but rather that a string matches a pattern. 它不是说一个字符串像另一个字符串,而是说一个字符串匹配一个模式。

$inksetc = htmlentities($_GET['ID']);// need to store LC41 in $_GET['ID'].

  $sql2 = "SELECT Id,   Product_Type,  Product_No ,  miniImageFarm

     FROM `Canon_INK`

     WHERE Id IN
         (SELECT Id FROM Canon_INK WHERE Id LIKE '%".$inksetc."%') // use Id instead of Id= $inksetc.

     LIMIT 4";

$result = mysqli_query($dbc,$sql2) or die(mysqli_error($dbc));

 while($rows = mysqli_fetch_array($result)){

?> ?>

I'm assuming that the "LC41 Black" type value is stored in the Product_No column - not the Id column. 我假设“ LC41 Black”类型值存储在Product_No列中,而不是Id列中。 I'm also assuming that you don't want the original product to show up in the list of similar products. 我还假设您不希望将原始产品显示在类似产品的列表中。

You can therefore join the table with itself to get the results, rather than using a subquery: 因此,您可以将表与其自身连接以获得结果,而不是使用子查询:

SELECT  c.Id,  c.Product_Type,  c.Product_No,  c.miniImageFarm
FROM Canon_INK AS c
INNER JOIN 
    Canon_INK AS OriginalProduct
ON 
    SUBSTR(c.Product_No, 0, 4) = SUBSTR(OriginalProduct.Product_No, 0, 4)
AND
    c.Id != OriginalProduct.Id
WHERE
    OriginalProduct.Id = $intsect
LIMIT 4;

Your query is insecure at the moment - please do not escape things for SQL using the htmlentities function, instead have a look at mysqli_real_escape_string . 您的查询目前不安全-请不要使用htmlentities函数对SQL进行转义,而应查看mysqli_real_escape_string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM