PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 20, 2022

[FIXED] PHPMyAdmin Match rows with two or more strings

 January 20, 2022     database, phpmyadmin, row, sql     No comments   

Issue

I have a huge database and inside that database I want to find rows from the column 'title' that doesn't contain the words "GmbH", "GbR", "e.K.", etc.

I tried this:

SELECT * FROM `listings` WHERE `listings`.`title` NOT LIKE '%GmbH' AND `listings`.`email_allow`=0

this works but this shows me all other rows with these other words so how can I write a single query that shows me only the rows that do not contain these words.

These queries were unsuccessful:

   1. SELECT * FROM `listings` WHERE `listings`.`title` NOT LIKE '%GmbH' OR '%GbR' OR '%AG' AND `listings`.`email_allow`=0
   2. SELECT * FROM `listings` WHERE `listings`.`title` NOT LIKE ('%GmbH' OR '%GbR' OR '%AG') AND `listings`.`email_allow`=0
   3. SELECT * FROM `listings` WHERE (`listings`.`title` NOT LIKE '%GmbH' OR '%GbR' OR '%AG') AND `listings`.`email_allow`=0

Solution

You need to repeat the NOT LIKE:

SELECT l.*
FROM `listings` l
WHERE l.title NOT LIKE '%GmbH' AND
      l.title NOT LIKE '%GbR' AND
      l.title NOT LIKE '%AG' AND
      l.email_allow = 0;

You might want to consider a full text index, if you have queries like this. LIKE can be pretty expensive.

You can also do this using regular expressions:

SELECT l.*
FROM `listings` l
WHERE l.title NOT REGEXP '(GmbH|GbR|AG)$' AND
      l.email_allow = 0;

Regular expressions will not necessarily be faster, but it is more concise.



Answered By - Gordon Linoff
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing