0

好的,我有 mysql 查询,我想在 table_1 中搜索这两个参数
我会尝试用“php”展示这个想法

if(table_2_params.params_id == 124){
   //select LIKE  "%EXTEC (Sandvik)%" from  table_2_params.nosaukums
}
elseif(table_2_params.params_id == 126){
  //SELECT  FROM table_2_params WHERE nosaukums is BETWEEN  "2010" AND  "2012"
}

我想查询显示第一个和第二个参数...(他们都匹配
天我希望你能明白我想说什么...

好的,这是 mysql查询:

 SELECT table_1 . * , table_2_params . * 
    FROM table_1, table_2_params
    WHERE table_1.pamat_kat_sad =  '756'
    AND table_1.preces_id = table_2_params.preces_id
    AND (
        (
        table_2_params.params_id =124
        AND table_2_params.nosaukums LIKE  "%EXTEC (Sandvik)%"
        )
        AND (
        table_2_params.params_id =126
        AND table_2_params.nosaukums
        BETWEEN  "2010"
        AND  "2012"
        )
    )
    ORDER BY table_1.preces_id DESC

问题可能是table_2_params.nosaukums类型是longtext......

如果我只选择

    AND (
    table_2_params.params_id =126
    AND table_2_params.nosaukums
    BETWEEN  "2010"
    AND  "2012"
    )

它显示正确的年份......如果我只选择

(
    table_2_params.params_id =124
    AND table_2_params.nosaukums LIKE  "%EXTEC (Sandvik)%"
    )

它还显示正确的值...

但我不能让他们俩一起工作..

请帮我!

谢谢!

4

2 回答 2

0

问题出在这里:

 AND (
    (
    table_2_params.params_id =124          #remove this check
    AND table_2_params.nosaukums LIKE  "%EXTEC (Sandvik)%"
    )
    AND (
    table_2_params.params_id =126          #or remove this check
    AND table_2_params.nosaukums
    BETWEEN  "2010"
    AND  "2012"
    )
)

您尝试比较 params_id 是否同时为 124 和 126。那将永远不会起作用。删除其中一个比较,您的查询应该可以工作!:)

于 2012-10-03T07:04:03.137 回答
0

如果您想要两组条件,只需使用OR. 您还应该注意,表之间的连接应该使用(INNER|LEFT) JOIN

SELECT
    table_1.*
    , table_2_params.* 
FROM
    table_1
    INNER JOIN table_2_params
        ON table_1.preces_id = table_2_params.preces_id
WHERE
    table_1.pamat_kat_sad =  '756'
    AND
    (
        (
            table_2_params.params_id =124
            AND table_2_params.nosaukums LIKE '%EXTEC (Sandvik)%'
        )
        OR
        (
            table_2_params.params_id =126
            AND table_2_params.nosaukums BETWEEN '2010' AND '2012'
        )
    )
ORDER BY
    table_1.preces_id DESC

最后,您的查询可能表现不佳,因为 mysql 将无法使用任何索引AND table_2_params.nosaukums LIKE '%EXTEC (Sandvik)%'

于 2012-10-03T07:13:40.343 回答