Complex MySQL query with (slightly) different requirements

I have a function with a complex MySQL query over multiple tables where I check many conditions such as not-deleted, active, language, date, access rights and so forth. So it already has multiple if-else in it.

Now I need to create another query very similar to the existing one where I query only specific categories with a different order and another one where I search for keywords in the title with a relevance calculation.

You can see two similar queries here: and (The table and column names are predefined. I cannot change anything there.)

I don't want to add even more if-else or copy & paste the existing query because it would create a lot of redundant and/or hard-to-maintain code.

Is there a design pattern that approaches this problem?


i am not so much clear about your requirement. i think this will may help you.

you can use first query as inline query as table in your second query but still copy paste is reburied.

Need Your Help

What went in creating

jquery html5 css3

I'm learning the basics of Jquery, HTML5 and CSS3. Just crossed this website

Primary Key in bound datagridview displayes negative numbers datagridview sql-server-2012

I have a datagridview which is bound to table in sqlserver 2012 database.