Topic on Project:Support desk

How to use the OR operator in queries?

6
184.155.109.183 (talkcontribs)

Hi, I'm having a bit of trouble getting my query to work properly.

Basically, I need to use an OR operator - Which seem non-existent in Mediawiki unless you use the query function. The issue is, however, that the query function doesn't recognize shared database tables - Which I need.

Here's how my query looks:

SELECT post_id FROM `blog_posts` WHERE type = "published" AND '.$comp_author.' = "'.$author_get.'" AND ('.$comp_tag_1.' = "'.$tag_get.'" OR '.$comp_tag_2.' = "'.$tag_get.'" OR '.$comp_tag_3.' = "'.$tag_get.'" OR '.$comp_tag_4.' = "'.$tag_get.'" OR '.$comp_tag_5.' = "'.$tag_get.'") ORDER BY post_datetime DESC LIMIT 6'.$f_offet.'

How can I make use of the OR operator using Mediawiki's query system?

Florianschmidtwelzow (talkcontribs)

I think you mean the database abstraction layer, yes? If so, you can simply submit your conditions as a string, instead of an array. If you don't want to build the list manually, you can use the DatabaseBase::makeList method with the second parameter "LIST_OR", see the docs for more documentation of this:

https://doc.wikimedia.org/mediawiki-core/master/php/html/classDatabaseBase.html#a8bc0559631db5130e6841b5e5873ac3e

And for the select method itself:

https://doc.wikimedia.org/mediawiki-core/master/php/html/classDatabaseBase.html#a76f9e6cb7b145a3d9020baebf94b499e

184.155.109.183 (talkcontribs)

Thank-you for your reply!

It seems that, oddly, makeList doesn't work within special pages, I keep getting:

Call to undefined method SpecialViewPosts::makeList()

Does makeList not work in Special Pages? Is there anyway to make my own 'list' without it?

184.155.109.183 (talkcontribs)

...And just to be clear, if there are any makeList alternatives, I'd still need to OR function to work in some form.

184.155.109.183 (talkcontribs)

...And I apologize for a third reply, but if makeList CAN work within SpecialPages, then how would that look in a query, exactly? My query + the list look like this at present:

  $conds1 = array($comp_tag_1 => $tag_get,
       $comp_tag_2 => $tag_get,
       $comp_tag_3 => $tag_get,
       $comp_tag_4 => $tag_get,
       $comp_tag_5 => $tag_get);

  $conds_var1 = $this->makeList( $conds1, LIST_OR );

       $dbr = wfGetDB(DB_SLAVE);
	$result = $dbr->select('blog_posts', array(
	'post_id'
	) , array(
	'type' => 'published',
	$comp_author => $author_get,
        ($conds_var1)
	) , __METHOD__, array(
	'ORDER BY' => 'post_datetime ASC DESC LIMIT 6 $f_offet'
	));

Aside from the fact that makeList isn't working right now, is that query even correct?

Florianschmidtwelzow (talkcontribs)

makeList isn't a method of SpecialPage, it's a method of DatabaseBase. So use $dbr->makeList( $conds, LIST_OR );

And the condition parameter expect one array i think. I don't know, what happens, when you try to give two arrays. o please use a string if you want to combine or and and conditions. E.g. use $conds_and as a string for makeList( $and_conditions, LIST_AND ); and $conds_or as a string for makeList( $or_conditions, LIST_OR );

Reply to "How to use the OR operator in queries?"