Tell me how to make a selection of data from a table with several conditions. Now I have this:


I need to add another condition in the request. Adding one more AND at the end did not help. How to write correctly if you need to check the record in 3, 4 or 5 fields?

  • write as they wrote that did not help - frank
  • maybe you need OR? - Gorets
  • Not. It is necessary to make a sample with verification of 3 fields. - Saturn
  • @frank as I wrote in the question, tried to add another AND (specifically AND ACTIVE = Y). But it did not give the result. - Saturn
  • one
    in general, in this case, it would be necessary to do this: $ res = CIBlockElement :: GetList (array ("SORT" => "ASC"), array ("IBLOCK_ID" => 9, "CREATED_BY" => $ _ REQUEST ["userid" ], "ACTIVE" => "Y")); while ($ ar_fields = $ res-> GetNext ()) {// ... your work with the sample data} The reason is the bitrix and its tables are not constant for all versions and if you update it is not a fact that it will work you need to use the API - binliz

1 answer 1

I answer 2 questions at once. Quotes are required, as in the example I wrote

 SELECT `field` FROM `table` WHERE `field`='a' AND `field2`='b' AND `field3`='3' 

Write also through AND. The fact is that beginners are accustomed to without mandatory quotes, because in 99% of cases, a "ride". Take the habit of writing as I wrote. For example, when I write something long, I put the conditions in an array, and already in the request I make them implode.

I hope the most explained

  • And I take the conditions, if at all, in parentheses. It's not obligatory? - sinedsem
  • brackets for the other are needed. SELECT field FROM table WHERE field = '1' AND field2 = '2' AND ( field3 = '3' OR field3 = '4') // if so, then 1e and 2e and (3e or 4e), i.e. 1e and 2e exactly, 3e is one of two SELECT field FROM table WHERE field = '1' AND field2 = '2' AND field3 = '3' OR field3 = '4' // if so, then 1e and 2e and 3e or 4e, i.e. here is one of two things. 1: 1e and 2e and 3e, 2: 4e - frank
  • @Saturn, yes, I know, the logic passed in school. When I just started learning mysql, I just didn’t work with brackets around all the conditions, since then I always take all the conditions into brackets ... - sinedsem
  • Edrit, really useful and correct habit, I just helped. As with SELECT and UPDATE. - Tokwiro