dpgbh20688 2017-01-01 13:15
浏览 14
已采纳

从2或3个表中搜索SQL

I have a search form where I can search for my webshop products.

1 product can be in multiple categoris, not just in one. I store this in the termek_katgoria_kapcsolo table. At insert, it creates as many lines, as the product belong to many categoria.

Example: The ID 12 product belong to ID 1, ID 2, ID 3 categoria.

The search sql only look at categoria, when one categoria is selected. Most often, I just search for the products name, I don't sort it to categoris.

How can I write the sql, that if I select a categoria also? I show you the tables on a pic.

if($termek_kategoria == 0 ) // Sort to categoria or not only search for product name, id...
{
  $sql = "
    SELECT termek_id, termek_nev, termek_cikkszam, termek_status FROM termek
    WHERE $kereses_helye LIKE '%$kw%' ORDER BY $kereses_rendezes $kereses_sorrend
  ";
}
else
{
  // Sorting for categoria also
  $sql = "
    SELECT termek_id, termek_nev, termek_cikkszam, termek_status FROM termek
    WHERE $kereses_helye LIKE '%$kw%' AND termek_kategoria =
    '$termek_kategoria' ORDER BY $kereses_rendezes $kereses_sorrend
  ";
}

enter image description here

Update:

$sql = "
  SELECT termek.termek_id, termek.termek_nev, termek.termek_cikkszam, termek.termek_status
  termek_kategoria_kapcsolo.*, termek_kategoria.kat_id
  FROM termek
  LEFT JOIN termek_katgoria_kapcsolo ON termek_kategoria 
  WHERE termek_kategoria_kapcsolo.kat_kapcs_kategoria_id = termek_kategoria.kat_id
  AND termek.termek_id IN (SELECT kat_kapcs_termek_id FROM
  termek_kategoria_kapcsolo WHERE kat_kapcs_kategoria_id = '$termek_kategoria')
";

This result: enter image description here

Whats going wrong here?

What I want is when I select a categoria, the program give me the products, that are in the selected categoria.

  • 写回答

1条回答 默认 最新

  • dream04110 2017-01-01 15:33
    关注

    I solved the problem:

    $sql = 
                       "
                            SELECT 
                                t.termek_id, 
                                t.termek_nev, 
                                t.termek_cikkszam, 
                                t.termek_status,
    
                                kapcs.kat_kapcs_kategoria_id,
                                kapcs.kat_kapcs_termek_id
    
                                FROM termek t
    
                                LEFT JOIN termek_katgoria_kapcsolo kapcs ON kapcs.kat_kapcs_kategoria_id = '$termek_kategoria' 
    
                            WHERE t.termek_id = kapcs.kat_kapcs_termek_id AND t.$kereses_helye LIKE '%$kw%' ORDER BY t.$kereses_rendezes $kereses_sorrend                           
                       ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗