user1852176
user1852176

Reputation: 455

Selecting value once with multiple entries MySQL and PHP

A user can create a form that allows other users to register. So, if a user creates a form, he can also register on said form.

Say I have the following tables

//Form 
id | title  | entry             | userid
1     test     entry for form        1

//form_register - Allows users to register for a particular form, form_id references form.id
form_id | userid
   1        1
   1        1
   1        2
   1        2

Now, suppose I want to generate a list for forms users have created AND signed up for. Users can sign up for the same form more than once, but I only want to display the form once. I don't want a list of every single time a user signs up for, only a list of the forms they signed up for, not the actual signups.

If a user creates a form, then signs up for that same form 2 times I only want the form information to display 1 time. My current query displays the form info 3 times (one for creating, 2 for registering).

    SELECT form.*, form_register.* FROM forms
    INNER JOIN form_register on form_regiseter.form_id = form.id
    WHERE form_register.userid = '$userid'

When this query runs for user 1, it generates the following

Title  
test
test
test

I just want it to display the form once like this

Title
test

For userid2 this would be the output

Title
test
test

And I want it to be

Title
test

Upvotes: 0

Views: 486

Answers (2)

Stefan
Stefan

Reputation: 2204

SELECT DISTINCT form.*, form_register.* FROM forms
    INNER JOIN form_register on form_regiseter.form_id = form.id
    WHERE form_register.userid = '$userid' 

may be what you're looking for

Upvotes: 1

Halcyon
Halcyon

Reputation: 57721

Use DISTINCT or GROUP BY to get unique rows.

Alternatively, you could also disallow duplicate entries in the form_register table by applying a UNIQUE-index. You can rewrite your INSERT queries to REPLACE queries and you will never get duplicate rows.

REPLACE will INSERT a row if it doesn't exist, or replace it (based on the PRIMARY KEY) if it does.

Upvotes: 1

Related Questions