如何使用Like运算符在Mysql中不显示重复值?

时间:2017-09-06 01:39:29

标签: mysql sql

这是我正在使用的mysql查询。

SELECT distinct Emprego.title as title, Emprego.up as up, Emprego.datetime as datetime, Emprego.content as content, Category.name as category_name, Emprego.states as states, Emprego.id as id, User.nome as nome, Emprego.email as email FROM City, User , Category, Emprego, Type
    where
     Emprego.category_id=Category.id and Emprego.City_id=City.id and Emprego.type_id=Type.id and Type.id='$type'
     and  (Emprego.title LIKE '%$text%') or (Emprego.content like '%$text%') and Emprego.User_id= User.id
    order by datetime DESC

我正在使用此查询获取重复的行。

如何解决???

结果如下:

[
    {
        "title": "Cozinheiro Madeira",
        "up": "1",
        "datetime": "2017-09-05 23:41:48",
        "content": "Cozinheiro na \nMadeira",
        "category_name": "Telecomunicações",
        "states": "2",
        "id": "12",
        "nome": "admin",
        "email": "admin@getal.con"
    },
    {
        "title": "Cozinheiro Madeira",
        "up": "1",
        "datetime": "2017-09-05 23:41:48",
        "content": "Cozinheiro na \r\nMadeira",
        "category_name": "Telecomunicações",
        "states": "2",
        "id": "16",
        "nome": "admin",
        "email": "admin@getal.con"
    }
]

2 个答案:

答案 0 :(得分:0)

由于以下因素,您可能会遇到问题:

(Emprego.title LIKE '%$text%') or (Emprego.content = '%$text%') and Emprego.User_id= User.id

你的意思是:

(Emprego.title LIKE '%$text%' or Emprego.content = '%$text%') and Emprego.User_id= User.id

答案 1 :(得分:0)

您的查询编写得很糟糕,它建议您向您展示如何编写可敬的SQL查询。 从不JOIN子句中使用逗号。 始终使用正确的JOIN语法:

SELECT distinct e.title, e.up, e.datetime, e.content,
       c.name as category_name, e.states, e.id,
       u.nome as nome, e.email as email
FROM Category c JOIN
     Emprego e
     ON e.category_id = c.id JOIN
     City ci
     ON e.city_id = ci.id JOIN
     User u
     ON e.User_id = u.id JOIN 
     Type t
     ON e.type_id = t.id
WHERE e.title LIKE '%$text%' or e.content = '%$text%'
ORDER BY e.datetime DESC;

目前尚不清楚是否真的需要SELECT DISTINCT。由于WHEREAND运算符的优先级,这也解决了OR子句中的问题。