在查询中使用LIKE子句

时间:2013-12-20 12:20:53

标签: php mysql dynamic sql-like

我正在尝试编写一个动态查询,它从多个下拉列表中获取信息。 在下拉列表中有一个名为ANY的选项,我给出了值%,以便在选择它时,它不会影响查询选择。

这是我的疑问:

$query_pag_data = "
SELECT *
FROM forecast
WHERE QuoteCode IN
    (SELECT QuoteCode
     FROM `StockData`
     WHERE AssetType LIKE '$sec'
       AND region LIKE '$indust'
       AND exchange LIKE '$exchange'
       AND Country LIKE '$cntry')
  AND RANKING LIKE '$sig_m_t'
  AND RANKINGw '$sig_l_t'
  AND NewSigD LIKE '$new_m'
  AND NewSigW LIKE '$new_l'
ORDER BY SCORE DESC LIMIT $start,
                          $per_page
";

如果我在变量中传递%,我想知道它是否有效?

1 个答案:

答案 0 :(得分:2)

我在这些案件中所做的是:

$query_pag_data = "
SELECT *
FROM forecast
WHERE QuoteCode IN
(SELECT QuoteCode
FROM `StockData`
WHERE AssetType LIKE '%$sec%'
AND region LIKE '%$indust%'
AND exchange LIKE '%$exchange%'
AND Country LIKE '%$cntry%'
AND RANKING LIKE '%$sig_m_t%'
AND RANKINGw '%$sig_l_t%'
AND NewSigD LIKE '%$new_m%'
AND NewSigW LIKE '%$new_l%'
ORDER BY SCORE DESC LIMIT $start,$per_page";