SQL table structure for questions/answer system? - php

I am making a question posting system. Each question post must contain the Users ID/username and the unique question id
MY TABLES
------------USERS---------------------------------
id
uname
password
fname
lname
-----------QUESTIONS-----------------------------
user_id
question_id
question
description
time
I am a beginner here, please put up with me. I just don't know how I can match my users_ID with question id as they are on separate tables.That way when a question is posted I can retrieve the users ID who posted the question alongside with the question id.
My rationality with combining the tables will not work simply because I can't think of a way to match a users unique id with the question_id. Please help me...really lost

You can use Inner Join to display the matched records in between the two tables:
SELECT
question_id,
question,
id,
uname
FROM Questions A INNER JOIN Users B
ON A.user_id=B.id

If you want to show up your question data with an user_id, it associated with user and question table. you can use inner join like this.
For show up your question data :
select * from questions inner join user on question.user_id=user.id;

Related

Select all from a table ORDER BY another tables column, with match primary key [duplicate]

This question already has answers here:
PHP/MySQL Order by column in a different table
(2 answers)
Closed 5 years ago.
I've been looking for a while but the query I am trying to accomplish seems fairly hard to find any information or documentation on how to do what I am trying to do.
I have two tables, one of them stores my user accounts and basic information. I then have a second table that holds a little more information about the user.
Both of these tables have primary keys (table one is id and table two is user_id) which I use to know who is who and match records between both tables.
What I am trying to do today is I want to get 10 records from table one, order by a column in table two (room_count) DESC.
Table #1's name is "users" and Table #2's name is "user_information".
What have I tried?
I'm not really sure where to start so I haven't tried anything yet.
How would I got about doing something like this?
Thank you to any answers posted.
For example, let's say I have 4 users, I'll write the username followed by the room_count column in the other table below.
Adam Sandler : 4
Jenny Hang : 9
Peter Foreign : 0
If I was to use the query with ASC it would start with Peter Foreign and end with Jenny Hang
Don't you just need a simple join?
SELECT
FROM users
INNER JOIN user_information ON users.id = user_information.user_id
ORDER BY user_information.room_count DESC
LIMIT 2
Please try this:
SELECT *
FROM users u
INNER JOIN user_info ui
ON u.id = ui.user_id
ORDER BY ui.room_count DESC
LIMIT 10
Try something basic like
select users.*
from
users, user_information
where
users.id =user_information.user_id
order by
user_information.room_count
desc
Limit 10
Edit: changed select users.id to select users.* to better fit the question asked.

Join tables when fields aren't equal - PHP MySQL

I created two tables, one stores the questions of a quiz, and the other one stores all the answers, that users made.
The first table called "questions" contains the questions:
Field names: id|question
Eg. contents:
1|what's your fav color?
2|what's your fav animal?
The second table named "answers" stores all the answers, that users made:
Fields names: id|questionid|userid|answer
Eg. contents:
1|1|1|Red
1|1|3|Magenta
1|1|4|Green
I'd like to select those questions, that haven't been answered yet by a user.
I store the current user's id in a $_SESSION['id'] session. I tried so many ways, to get these questions, the closest query I've made, was this:
$query = SELECT questions.*, answers.* FROM questions LEFT JOIN answers ON questions.id=answers.questionid WHERE answers.id IS NULL OR answers.userid <> '.$_SESSION['id'];
This won't work, because if there's another userid in the answers table at the same question id, it still selects that row. What could be the problem? Where did I mess up my query?
Thanks in advance for all of your help!
Your user condition is in the wrong place. Since you'll want to try to find a match between the specific user and the question and detect a non match, the user part needs to go inside the ON clause with a null check in the WHERE clause;
SELECT q.*
FROM questions q
LEFT JOIN answers a
ON q.id = a.questionid
AND a.userid = YOUR_USER_ID
WHERE a.id IS NULL
An SQLfiddle to test with.

MySQL - SELECT / JOIN from two tables - inverted or negative

i want to make survey on my site and i want to every survey question is displayed to user (registered and logged in) only once (if answered). So i made 2 tables.
surveyQuestions (table 1 with data for each questions):
surveyId
question
etc
surveyAnswers (table 2 with answers of each user to questions):
surveyId (= surveyId from table 1)
userId
etc...
What i need is to select 1 question from table surveyQuestions which has not been yet answered from logged user (answers are stored in surveyAnswers). UserId in browser is handled using $_SESSION['id'].
I have tried different JOIN methods, but without luck and i am lost now.
Thank you very much.
assuming you have distinct questions in your questions table and multiple duplicates of each question for different users in your answers table you should be able to do it like this.. get survey id's for all answered questions for a particular user and then look at the questions they havent answered by using NOT IN
SELECT whatever_you_want
FROM surveyQuestions
WHERE surveyId NOT IN(
SELECT surveyId
FROM surveyAnswers
WHERE userId = $id -- # -- whatever your filtering id is here
)
LIMIT 1

select field from one table and match it in another table and return vales [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i have 2 tables
categories
categories (cid, year_id, category_title)
users
users (uid, year_id, name, rollno, password)
when user login with his rollno and password i want to show his category by
selecting year_id from 'user' and matching for that year_id in 'categories'
i'm trying
SELECT * FROM categories WHERE (select year_id='$year' from users)
Use a simple join
SELECT c.* FROM categories c
JOIN users u ON(c.year_id =u.year_id)
WHERE u.year_id='$year'
Edit
SELECT c.*,u.year_id user_year FROM categories c
JOIN users u ON(c.year_id =u.year_id)
WHERE u.uid='$user_id'
If the year_id is the same for both, and you know the year_id that was returned from the users table, just use that value in a select on the categories table.
SELECT *
FROM `categories`
WHERE `year_id` = '[year_id from users data]'
No join needed.
UPDATE:
To validate the user login:
SELECT *
FROM `users`
WHERE `rollno` = '[users rollno]'
Then compare the password returned from that query to the password provided by the user, and if they match, you know the user is valid, and can use the year_id that was also returned to grab the categories.

count as part of a query

If I have a users table and a question table, where one user can have multiple questions, can I return, for example,
bob | 10 questions
sam | 2 questions
with one query?
Using php with pdo for what it's worth.
users table
userID
name
etc.
questions table
questionID
userID
question
flagged
answered
etc.
I want some fields from the users table and a count of the associated questions on the same row. if it can't be done I'll just use separate queries but I just thought I'd ask for the sake of having tidier code
Something like this:
SELECT u.*, COUNT(*) questions
FROM users u
JOIN QUESTIONS q ON u.userid = q.userid
GROUP BY u.userid
If you only want some columns from the users table, replace u.* with the list of columns you care about.
Assuming you have a table that contains one or many question for each user;
SELECT Count(DISTINCT id) FROM questions
// or
SELECT Count(id) FROM questions GROUP BY user_id
this is what Im talking about:
SELECT u.name, COUNT(*) questions
FROM users u
JOIN questions q ON u.userID = q.userID
GROUP BY u.name

Categories