I am trying to create a join statement to solve my problem but cannot get my head around it.
I am a new to join statements so please bear with me if my sql statement is nonsense.
I have two tables, one is a table of questions where users have asked questions about items for sale.
Second is a table of items that the user has asked a question about.
Table one called questions consists of question_ref, questioner_user_ref, item_ref, seller_ref, question_text, timestamp
questions
===========
+--------------+--------------------+---------+-----------+--------------+----------+
| question_ref |questioner_user_ref |item_ref |seller_ref |question_text |timestamp |
+--------------+--------------------+---------+-----------+--------------+----------+
Table two called my_item_comments consists of questioner_ref, item_ref, last_question_ref
my_item_comments
===========
+---------------+---------+------------------+
|questioner_ref |item_ref |last_question_ref |
+---------------+---------+------------------+
I have set up table two to keep track of items that the user has asked questions about so they can be informed when someone else asks the seller a question or the seller answers.
So I want to create a recordset of questions that
a). Someone has answered a question about an item the user is selling
b). A seller has replied to a question the user asked,
c). A third user has asked a question about an item that the user has also asked a question about.
A bit like facebook's commenting system, where you are informed about comments people have made on statuses that you have commented on.
So my current sql statement is as follows
$user_ref= current logged in user
$sql=mysql_query("
SELECT * FROM questions
LEFT JOIN my_item_comments
ON questions.item_ref=my_item_comments.item_ref
WHERE questions.questioner_user_ref!='$user_ref'
AND (questions.seller_ref='$user_ref' OR questions.item_ref=my_item_comments.item_ref
ORDER BY timestamp DESC");
The results don't work and I think its because of the OR questions.item_ref=my_item_comments.item_ref but for the life of me I cannot work it out.
Any help would be greatly appreciated, even if it means restructuring my database with more tables or new fields in the tables.
thanks in advance, Barry
SELECT * FROM questions
LEFT JOIN my_item_comments ON questions.item_ref=my_item_comments.item_ref
WHERE
questions.questioner_user_ref != '$user_ref' AND
(questions.seller_ref='$user_ref' OR questions.item_ref=my_item_comments.item_ref)
ORDER BY timestamp DESC
I think you were missing a ) to enclose the OR clause (in the example above I added it)
You should try to improve readability for yourself and us so that it is easier to debug and easier for us to help you :-)
$q = "
SELECT *
FROM questions
RIGHT JOIN my_item_comments ON questions.item_ref = my_item_comments.item_ref
WHERE questions.questioner_user_ref != '".$user_ref."'
AND ( questions.seller_ref = '".$user_ref."'
OR questions.item_ref=my_item_comments.item_ref
)
ORDER BY timestamp DESC";
$rs = mysql_query($q);
And preferably just the query with dummy data
SELECT *
FROM questions
RIGHT JOIN my_item_comments ON questions.item_ref = my_item_comments.item_ref
WHERE questions.questioner_user_ref != 'dummy_ref'
AND ( questions.seller_ref = 'dummy_ref'
OR questions.item_ref=my_item_comments.item_ref
)
ORDER BY timestamp DESC
Try a right join for your query. Left join will indeed get all the rows.
Related
I have page called questions where the user gets asked questions and he/she has the option to answer them. The questions are pulled from a table called questions. When a question gets answered, a table in my database called answered_questions registers the id of the question answered and the id of the user who answered the question. The purpose of this is to hide the answered questions when the user accesses the page again.
On page load I'm trying to join the two tables and see if the question_id exists in both tables where the userID is that of the logged in user. If the id does exist in both tables then it shouldnt display the result per the use of <>. Problem is that its looping several times for each iteration when I try the following query:
SELECT questions.question_id, questions.user_id
FROM `questions`
JOIN `answered_questions`
ON questions.question_id <> answered_questions.question_id
WHERE answered_questions.user_id = ".$userID."
But it works fine when I use this
SELECT questions.question_id, questions.user_id
FROM `questions`
JOIN `answered_questions`
ON questions.question_id **=** answered_questions.question_id
WHERE answered_questions.user_id = ".$userID."
I sense that I'm doing something wrong with the logic of it all. Any help or clues would be highly appreciated.
To get unanswered questions You can use LEFT JOIN:
SELECT questions.question_id, questions.user_id
FROM questions
LEFT JOIN answered_questions
ON answered_questions.question_id = questions.question_id
AND answered_questions.user_id = ".$userID."
WHERE answered_questions.question_id IS NULL
I am trying to get the following data from 3/4 tables in 1 Mysql query, wondering if it is possible ? The tables are
TOPIC
topicid (FK)(PK)
groupid
topic
user
LIKED
likeid
topicid (FK)
user
COMMENT
commentid (PK)
topicid (FK)
comment
user
I write my topics and store in TOPIC Table with unique topicid. I group each topic using groupid.
Other tables may have 0 or more data per topicid.
I am trying to get each topic for a particular group and also get other datas from the concerned Tables. I checked How to left join multiple one to many tables in mysql? and got few idea but that is for the count while I wanted to get details from that table (users who like), and (user and their comment).
I have tried
SELECT t.topicid,
topic,
group_concat(DISTINCT likeid,l.user SEPARATOR '|'),
group_concat(DISTINCT commentid,comment,c.user SEPARATOR '|') AS comments
FROM TOPIC t
LEFT JOIN LIKE l ON l.topicid = t.topicid
LEFT JOIN COMMENT c ON c.topicid = t.topicid
WHERE t.groupid='some_value'
GROUP BY t.topicid
While this works partly e.g. I do get the details but only if there is one topic in a group. If there are 2 or more topics in a group then the concat details are stored in the first record only and the later topics show no likes and comments.
Can someone please help me to correct this or any particular Mysql function I am missing
I am very very sorry for wasting your time, after thorough re-check I found my table data were wrong (checked after making sqlfiddle thanks #Barmar).
I was by mistake inserting wrong data in like and comment table. So Likes and comments for 2nd topic topicid='2' of groupid='1' were inserted by mistake as topicid='1' that is why the details only showed in 1st topic and nothing came out for second topic.
The SQL above is absolutely correct and thankyou for helping me find the fault.
Extremely sorry for posting again.
I have a MYSQL 'activity' table where all activity by a user gets logged. Let's say that a user's activities can be either to ask a question or answer a question. So if a user asks a question, their question gets put into the 'question' table and also the activity gets put into the 'activity' table. Same thing if user provides an answer, except that the answer goes into the 'answers' table and the 'activity' table.
The question table's fields are:
q_id question
The answers table's fields are:
a_id q_id answer
The activity table's fields are:
activity_id q_id user_id action_type
(There are more columns then shown above, but these illustrate the point.)
What I want to be able to do is show a user's recent actions on the site, and actually show what the actions were.
My current MYSQL query simply joins the activity table with the question table as follows (using PDO):
"SELECT *
FROM activity
LEFT JOIN question ON activity.q_id = question.q_id
WHERE activity.user_id = :user_id
ORDER BY activity.a_id desc"
This works okay, and can always give me the question for display in the activities list. However, if the activity that is returned is an answer, I would like to be able to show the answer as well. I tried just using a triple join as follows:
"SELECT *
FROM activity
LEFT JOIN question ON activity.q_id = question.q_id
LEFT JOIN answers ON activity.q_id = answers.q_id
WHERE activity.user_id = :user_id
ORDER BY activity.a_id desc"
But this just gives me a list of rows that is twice as long as the first query, and does not display the answers when the action_type was an answer.
I figure this should be possible, but the more I think about it I believe that maybe I should scrap the activity table and just perform the query directly on the question and answers tables with a UNION? Any opinions on this decision, and any thoughts on the queries?
You could use an outer join, which would return everything in the activity table, even if there isn't a corresponding row in the answer table:
SELECT *
FROM activity
LEFT JOIN question ON activity.q_id = question.q_id
LEFT OUTER JOIN answers ON activity.q_id = answers.q_id
WHERE activity.user_id = :user_id
ORDER BY activity.a_id desc
I'm assuming that for every q_id value in the activity table, there's always going to be one corresponding row in the question table.
To fix the problem with the duplicate answers, I'd add an a_id field to the activity table. For the question activity, you'd set it to NULL, and for each answer you'd set the a_id field to the appropriate a_id value to identify which answer goes with the activity. You'd then adjust the SQL above as follows:
SELECT *
FROM activity
LEFT JOIN question ON
activity.q_id = question.q_id
LEFT OUTER JOIN answers ON
activity.q_id = answers.q_id AND
activity.a_id = answers.a_id
WHERE activity.user_id = :user_id
ORDER BY activity.a_id desc
I'v been asked to add functionality to an application that lets users vote between two options: A and B. The table for those questions is very basic:
QUESTIONS
question_id (PK)
option_id_1(FK)
option_id_2(FK)
urgent (boolean)
Each time a user votes, that the user has voted is stored in an equally simple table:
USER VOTES
vote_id (PK)
user_id (FK)
question_id (FK)
The algorithm for selecting which question appears when a user requests a new one is complex, but for our purposes we can assume it's random. So, the issue?
Each user will be voting on many questions. Likely hundreds, and possibly thousands. I need to ensure no user is presented with a question they've already voted on, and the only way I can think to do that will, I'm guessing, pound the server into oblivion. Specifically, something like:
SELECT * from questions WHERE question_id NOT in (SELECT question_id from user_votes WHERE user_id = <user_id>) ORDER BY RAND() LIMIT 1.
[Note: RAND() is not actually in the query - it's just there as a substitute for a slightly complex (order_by).]
So, keeping in mind that many users could well have voted on hundreds if not thousands of questions, and that it's not possible to present the questions in a set order...any ideas on how to exclude voted-on questions without beating my server into the ground?
All advice appreciated - many thanks.
JOIN operator perform much better than nested queries in MySQL(that might have changed with latest MySQL releases but if you are experiencing performance problems than i guess my statement still holds)
what you could do is simply left join votes onto questions and only pick those records where none votes were joined(nobody voted):
SELECT *
FROM questions q
LEFT JOIN user_votes uv ON
uv.question_id = q.question_id AND
uv.user_id = '<user_id>'
WHERE vote_id IS NULL
RAND() is nasty however this may mitigate the problem while giving you the results you need. Seeing as you have mentioned that the RAND() is an example, I can't really provide more specific suggestions than that below however replacing the ORDER BY should work just fine.
The more you are able to limit the number of rows in the inner query, the faster the entire query will perform.
SELECT
q.*
FROM (
-- First get the questions which have not been answered
SELECT
questions.*
FROM questions
LEFT JOIN user_votes
ON user_votes.question_id = questions.question_id
AND user_votes.user_id = <user_id>
WHERE user_votes.user_id IS NULL
) q
-- Now get a random 1. I hate RAND().
ORDER BY RAND()
LIMIT 1
We have a web project, there are some multiple-choice questions and the registered users answer them...
I want to find the common answers of two users to give a comparison. I mean, the logged in user will go to some other user's profile page and see the comparison of their answers for their common questions only...
The tables are like (simplified):
questions
id
question
active -> enum('Y', 'N')
answers
id
question_id
answer
users
id
nick
user_answers
user_id
question_id
answer_id
private -> enum('Y', 'N')
I can join user_answers table with itself by giving an alias but I have to join other tables too. Private answers should not be taken into account, only active questions should be taken...
The site is expected to get some load so I'm afraid there are too many joins and where conditions. From my experiences I know that these complex queries can lock the tables and cause some serious performance issues, especially under heavy load...
So what would be the best practice when scalability and performance is taken into account...
Would smt. like sphinx or solar help, or any software based solution to do the comparison?
The results will be paginated by the way...
Right now I'm thinking of seperating question details and answer details and cache them, so the query will be smt. like:
select ua1.answer_id as her_answer_id,
ua2.answer_id as my_answer_id,
ua1.question_id
from user_answers ua1
inner join users_answers ua2 on ua1.question_id=ua2.question_id
where ua1.user_id=$herId
and ua2.user_id=$myId
and ua1.private='N'
order by ua1.question_id desc
questions.question and answers.answer will be taken from cache... In this case passive questions will be a problem but I think I'll try to move the passive questions yo some backup database which will complicate things...
I would use a conditional in the query:
select
user_answers.question_id `QuestionId`,
max(if((user_answers.user_id = my_id), user_answers.answer_id, 0)) `MyAnswer`,
max(if((user_answers.user_id = other_id), user_answers.answer_id, 0)) `OtherAnswer`
from users_answers
where user_answers.private = 'N'
and user_answers.user_id IN(myid, orderid)
group by users_answers.question_id
having count(*) = 2
order by user_answers.question_id
Haven't tested it, but you should get the idea!
Hope this works out for you...