0

Thanks for the time to read my question. I have a form on my website (Wordpress wp_forms) this generates a database entry wp_wpforms_entry_fields where the entries for the form are stored. My form contains three fields (Name, Email address and a Yes or No question).

In my database this creates the following table: enter image description here

What I'm trying to do is get the field_id from 1 and 3. So with my SQL I do the following:

$query = "SELECT * 
    FROM wp_wpforms_entry_fields 
    WHERE field_id = 1";

But this only selects the value from field_id 1 what I would like is to also have the value from WHERE field_id 3 = Yes. This way I can show the names of people who gave a positive answer to the question.

I have no idea where to start, this is probably not as difficult as I'm making it. But doing:

$query = "SELECT * 
    FROM wp_wpforms_entry_fields 
    WHERE field_id = 1 
        AND field_id = 3 
        AND IS Yes"; 

Surely doesn't work... thanks again for reading.

PS. I've left out the database connection rules because the connection works, I can retrieve data based on the first SELECT. But I can't seem to get the separation between Yes and No.

6
  • Then you need to select where entry_id = 8. Commented Oct 25, 2018 at 7:13
  • @u_mulder entry_id does not seem to have any relation with field_id Commented Oct 25, 2018 at 7:16
  • @ZainFarooq I presume entry in an entity which holds all fields result for certain action. Commented Oct 25, 2018 at 7:17
  • @u_mulder Then we don't need entry column at all Commented Oct 25, 2018 at 7:19
  • Maybe you're right. If OP needs all actions with yes answer, entry_id is not needed. Commented Oct 25, 2018 at 7:20

1 Answer 1

1

You'll have to join the table to itself:

select f1.value as name
from wp_wpforms_entry_fields f1
    inner join wp_wpforms_entry_fields f3 on (f1.entry_id = f3.entry_id and f3.field_id = 3)
where f1.field_id = 1 and f3.value = 'Yes'

In plain text, you're looking for names of entries, where a row for field_id 3 with same entry_id says 'Yes'.

Alternatively, with a subquery:

select value as name
from wp_wpforms_entry_fields f1
where field_id = 1
    and entry_id in (
        select entry_id from wp_wpforms_entry_fields f3 where field_id = 3 and value = 'Yes'
    )

Here we select a list of entry_ids for which there is 'Yes' in field 3, and then we select values of field 1 for those entries whose entry_id is on that first list.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for this. This is getting me every value from all table rows (so this shows me Name, E-mail address and Yes or NO). How do I only show the Name field if field_id 3 is Yes?
Fixed the query, I was missing some conditions.
This is going somewhere, I really appreciate your help. Final question because it's almost working. How do I echo the value from field_id 1. Because echo "-" . $row["value"]. "<br>"; does give me one value but echo's no values...
That query gives the result an alias name, so $row["name"].
Great stuff! This is working! Thanks a million! One final question, if you don't mind. Can I use this query to make a count for everybody that said yes? like select COUNT(value) as name
|

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.