我有以下2个表:
表1:发票
invoiceID clientName
| 1 |托尼
| 2 |路德维希
| 3 |拉尔夫
表2:invoice_items
invoiceID itemDescription
| 1 |平板显示器LED等...
| 1 | HP打印机喷墨打印机等...
| 2 |笔记本电脑wahtever等...
我正在尝试仅查询表格“发票”中的发票,但允许用户搜索商品说明。
我写过
$query = "SELECT i.*, ii.invoiceID, ii.itemDescription from invoices i, invoice_items ii, WHERE ii.itemDescription like "%'.$_GET["s"].'%" AND i.invoiceID = ii.invoiceID";
但这会导致重复。任何想法?
答案 0 :(得分:1)
如果你想要你可以调用distinct ...但在这种情况下你必须选择明确需要的列
$query = "SELECT distinct i.col1, i.col2, i.col3, ii.invoiceID, ii.itemDescription
from invoices i, invoice_items ii,
WHERE ii.itemDescription like "%'.$_GET["s"].'%"
AND i.invoiceID = ii.invoiceID";
你可以通过这种方式获得不同的结果(如果你的结果不需要描述)
$query = "SELECT distinct i.col1, i.col2, i.col3
from invoices i, invoice_items ii,
WHERE ii.itemDescription like "%'.$_GET["s"].'%"
AND i.invoiceID = ii.invoiceID";