php - Is there an efficient way to search variable parameters in mySQL with different results? -


I have a very complex question and due to specifications, I have not had any luck with any help with this.

I have to scratch for several months. Although in this question I will concentrate on only 3

  • Category ($ Cat)
  • Subcategory ($ sub)
  • Style ($ Normal)

MySQL column:

  id | Category | Subcategory | Style | Account | Date_received |  

(Account assigned: 1-3)

I have to return the result:

  • If only category Has been offered: first line account = 1 , then date_failed followed by date_modified (no prioritization of accounts 2 & Amp;; 3 )

  • If only category and subcategory is offered: First line account = 1 and

  • Li>

    If only category and subdue Categories and styles have been offered: First line account = 1 and account = 2 and account = 3 , then date_received < / Code>, date_modified

After this I have:

  if ($ cat and empty ($ Sub) and empty ($ General)) {$ run = mysql_query ("Select" by DB W here (category = '$ cat') order by case when account = '1' then 1 and 2 end, Greatest (date_failed) , Revised date, account) DESC "); } And if ($ cat and $ sub and empty ($ normal)) = $ 2 by order {$ run = mysql_query ("SELECT * FROM db WHERE" (category = '$ cat' and subcategory = '$ sub') 1 and 2 end, Greatest (date_date, modified date, account) DESC "); } And if ($ cat and $ sub and $ generic) {$ run = mysql_query (select "DB" from where * (category = '$ cat' and subcategory = '$ sub' 'and style =' $ General ') ORDER BY cases When Account = '3' then 1 and 2 End, Greatest (Date-Received, Modified Date, Account) DESC "); }  

Is it an effective way of searching and resizing different results?

Place all the conditions in an array and keep the preferred account in one variable.

  $ conds = array (); If ($ cat) {$ account = 1; $ Conds [] = "category = '$ cat'"; } If ($ sub) {$ account = 2; $ Conds [] = "subcategory = '$ sub'"; } If ($ General) {$ account = 3; $ Conds [] = "style = '$ General'"; } If (! $ Conds) {$ cond_str = implode ('AND', $ conds); $ Sql ​​= "SELECT * db to $ db WHERE $ cond_str if (account = $ account, 1, 2), Greatest (date_free, modified_date, account) deess"; $ Run = mysql_query ($ sql); ...}  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -