0
select * 
from suMain
where trackingMonth = '2018-07'
  and shopID = '421'
  and productName like '%a'
  and productName like '%x'
order by productName

This query is not working for me. Can anyone help, please?

Manoj
  • 19
  • 4
  • That's not java code, what type of SQL database are you using? – Woohoojin Jul 16 '18 at 17:33
  • 1
    You want both conditions to be satisfied with an one row? or else either of conditions to be satisfied with one row? – Nishan Iddamalgoda Jul 16 '18 at 17:39
  • 3
    What do you mean by "not working?" Do you get an error message? What error message? Do you see rows that you're not expecting to see? Do you *not* see rows that you *are* expecting to see? Be specific. – Ian McLaird Jul 16 '18 at 17:43
  • 3
    And on the off chance that it's not just a transcription error, you're missing an opening single-quote `'` character in `like '%x'` – Ian McLaird Jul 16 '18 at 17:45
  • if i remove one like '%a' then i have the output. but if i use both like then there is no output – Manoj Jul 16 '18 at 18:06
  • I want both conditions to be satisfied in one row – Manoj Jul 16 '18 at 18:07
  • 1
    @Manoj Think about your logic. `productname like '%a' and productName like '%x'`. Do you think you can get a result from this statement???? ProductName can only end with either 'a' or 'x', not both. – Eric Jul 16 '18 at 18:21
  • @Manoj - Seems your requirement is not clear. You should try to explain in plain English what you are trying to achieve. `productName like '%a'` means ends with letter a `productName like %x'` means ends with letter x Logically, you cannot satisfy both the conditions. You product name will end with some letter. It could be a or x. But it cannot be both. So when you remove `and productName like '%a'`, it works as you have got one or more products ending with letter x. – Bhavin Gosai Jul 16 '18 at 18:23
  • there are products wchich ends with a and x and both under this shopID and TrackingMonth – Manoj Jul 16 '18 at 18:25
  • if i says productname not like '%a' and productName not like '%x' this shows the correct output – Manoj Jul 16 '18 at 18:26
  • "*there are products wchich ends with a and x*" - that's impossible. There can only be one single character at the end. Or do you mean `%ax`? –  Jul 16 '18 at 19:18

1 Answers1

0

Try following:

select * 
from suMain
where trackingMonth = '2018-07'
  and shopID = '421'
  and (productName like '%a'
  or productName like '%x')
order by productName

Let me know if it works for you.

Bhavin Gosai
  • 211
  • 2
  • 6