Skip to content
Advertisement

SQL query with meta_key,meta_value and have 2 conditions

I am trying to do an SQL query to find a form id of the Registration form of the user on a wordpress site, because this form doesn’t have the id of the user yet, I have to find the user email which is unique and check if the gdprchekbox-1 is true because it is only used for the registering form so if I have both conditions I know that the form ID will be the registering form of the user.

So if both conditions are met I should only have one result of the entry_id form.

I have tried :

SELECT entry_id 
FROM `mod114_frmt_form_entry_meta` 
WHERE (`meta_key`, `meta_value`) IN (("email-1","emailofuser@mail.com"),("gdprcheckbox-1","true"));

Which was the closest I got but the IN clause makes it so it says OR and I would need both conditions to work.

SELECT entry_id 
FROM `mod114_frmt_form_entry_meta` 
where meta_key IN("email-1","gdprcheckbox-1") 
  AND meta_value IN("emailofuser@mail.com","true") LIMIT 1;

This one was close as well but it still doesn’t work well.

My table is as follows :

meta_id entry_id meta_key meta_value
– 1297 72 email-1 emailofuser@mail.com
– 1316 72 gdprcheckbox-1 true

I hope I was clear enough, thank you for your help ! 🙂

Answer

You only need to find 1 record, and only when gdprcheckbox-1 is true:

SELECT entry_id 
FROM `mod114_frmt_form_entry_meta` 
WHERE (`meta_key`, `meta_value`) = ("email-1","emailofuser@mail.com")
  AND EXISTS (SELECT 1 
              FROM `mod114_frmt_form_entry_meta` 
              WHERE (`meta_key`, `meta_value`) = ("gdprcheckbox-1","true")
             )