1

I have been trying to figure out how I can combine multiple values to see if one of them returns True then it exists. Meaning that if A is True and B is False = Return True. If both are False then of course we return = False.

What I have currently done is:

dict_tuple = {"type": "fruit", "link": link, "store": store}

sql_query = "SELECT store, link FROM public.fruit_urls WHERE link_type=%(type)s AND link=%(link)s AND store=%(store)s union SELECT store, link FROM public.store_items WHERE link=%(link)s AND store=%(store)s"

which seems to work but I would like to convert the query to use exists instead since I do check if its in the database or not.

I managed to solve to do one check:

sql_query = "SELECT EXISTS (SELECT store, link FROM public.store_items WHERE link=%(link)s AND store=%(store)s)"

but how can I combine both

  1. "SELECT EXISTS (SELECT store, link FROM public.store_items WHERE link=%(link)s AND store=%(store)s)"
  2. "SELECT store, link FROM public.manual_urls WHERE link_type=%(type)s AND link=%(link)s AND store=%(store)s"

together and then return if one of them are in the database or not

4
  • Simply put select exists(...) around your initial UNION query. Commented Apr 8, 2021 at 13:41
  • Do you mean like this? SELECT EXISTS ("SELECT store, link FROM public.fruit_urls WHERE link_type=%(type)s AND link=%(link)s AND store=%(store)s union SELECT store, link FROM public.store_items WHERE link=%(link)s AND store=%(store)s") ? @VadimLanda Commented Apr 8, 2021 at 13:43
  • Yes, exactly, only without the quotes. Commented Apr 8, 2021 at 14:00
  • Oh... That was as easy as that. I did not know that :) @VadimLanda - If you want, you could do that as an answer and Iwill mark it as the solution :) Commented Apr 8, 2021 at 14:07

1 Answer 1

1

Just surround SELECT ... UNION SELECT... with SELECT EXISTS(...):

SELECT EXISTS (
  SELECT store, link FROM public.fruit_urls WHERE link_type=%(type)s AND link=%(link)s AND store=%(store)s 
  UNION 
  SELECT store, link FROM public.store_items WHERE link=%(link)s AND store=%(store)s)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.