I have two tables questions table like below
and answers table like below
every question has multiple answers
How can I make query to return the result as below table
You have to order by two columns - q_id and ans_id. Since in question_tb there is no ans_id field you can put 0 instead.
select t.id, t.q_content from
(
select q_id, q_content, 0 k, q_id id from question_tb
union
select ans_q_id, ans_content, ans_id, ans_id from answer_tb
) t order by t.q_id, t.k
Hmmm.. I think you need to re-think your structure a bit: the last table (or query result) needs a column to designate which is the question, and which are the answers, and also to indicate the correct answer.
Also, I assume that each set of questions (and answers) will be tied to a particular user, so you will need a user_key in the answer table, as well.
SELECT Q.q_content AS question,
ANS.ans_content AS answer,
ANS.is_correct,
ANS.user_id,
FROM Question_TB Q
INNER JOIN tb_answer ANS
ON ANS.ans_q_id = Q.q_id
ORDER BY ANS.user_id, Q.q_id
Related
I am trying to count number of questions and answers for every company but I want to count them in a single query.
So the problem is this: table pitanje (question) is linked with tvrtka (company)
pitanje.tvrtka_id=tvrtka.tvrtka_id and table answer (odgovor) is linked with question table odgovor.pitanje_id = pitanje.pitanje_id
I have tried with something similar to this and got some faulty results (I counted question and answers separately for testing).
SELECT tvrtka.naziv,
(SELECT COUNT(*) FROM pitanje WHERE tvrtka.tvrtka_id = pitanje.tvrtka_id) AS brojPitanja,
(SELECT COUNT(*) FROM odgovor WHERE odgovor.pitanje_id = pitanje.pitanje_id) AS brojOdgovora
FROM tvrtka
ORDER BY tvrtka.tvrtka_id
But all I get is some MySQL errors (unknown column, not uniqe alias etc.) now and can't get even to a faulty results.
If someone can just explain me the concept of the code above. A lot of answers are written like this but I can't figure out how to do it on my example.
With englisch names it would be something like this:
SELECT
c.name,
COUNT(DISTINCT q.question_id) as numQuestions,
COUNT(a.answer_id) as numAnswers
FROM company c
LEFT JOIN questions q ON q.company_id = c.company_id
LEFT JOIN answers a ON a.question_id = q.question_id
GROUP BY c.name
I let it to you, to translate the table and column names back to your schema.
What is wrong with my code?
Here:
(SELECT COUNT(*) FROM pitanje WHERE tvrtka.tvrtka_id = pitanje.tvrtka_id) AS brojPitanja,
(SELECT COUNT(*) FROM odgovor WHERE odgovor.pitanje_id = pitanje.pitanje_id) AS brojOdgovora
in your second subquery with WHERE odgovor.pitanje_id = pitanje.pitanje_id you are trying to reference a table (pitanje) from another subquery. This is not possible. You can only reference a table in the FROM clause of the outer query, which is tvrtka.
This question already has answers here:
mysql extra columns with same name from two tables
(3 answers)
Closed 8 years ago.
I'm not even sure how exactly to ask this question so bear with me.
I have two tables, meets and locations, they are linked by the meet's 'loc' and the location's 'id'
I'm using this query:
$query = "SELECT * FROM meets LEFT JOIN locations ON locations.id=meets.loc ORDER BY date DESC";
$meets = mysqli_query($con, $query);
though it joins the table successfully I lose the meet's 'id' because it's being overwritten by the location's 'id'. So I end up with two identical entries 'id' and 'loc'.
Is there any way to avoid this because I need to call on the meet id?
do not select *, select the columns you need and rename them using the as key word like so
SELECT locations.id as loc_id, meets.id as meets_id, ... FROM meets LEFT JOIN locations ON locations.id=meets.loc ORDER BY date DESC
replacing ... with other columns you would like to select and possible renames of them.
You could alias the locations.id with a query like:
SELECT meets.*, locations.id AS loc_id FROM meets LEFT JOIN locations ON locations.id=meets.loc ORDER BY date DESC
You could add any other columns from locations that you might also need. You could further only select explicit columns from meet rather than meet.*
you could try to use SELECT meets.*, locations.id as locationId, locations.<row> as location<Row>, ...
Not sure if the syntax is exactly the same, but in SQL Server the syntax would be:
SELECT m.*, l.id as LocationId
FROM meets m
LEFT JOIN locations l
ON l.id=m.loc
ORDER BY date DESC
MySQLFiddle here: http://sqlfiddle.com/#!2/15d447/1
I have a single table I am trying to work with:
Table 1: user_answers table (stores users answers to various questions)
The notable values that are stored are the users id (column uid), the question id for the question they are answering (column quid), the answer to the question (column answer) and the importance of their answer (column importance).
The end result I want:
I'd like to be able to grab all the questions that any two users have answered, excluding any answers from questions that have either not been answered by the other party, or answers to the same question which have a value of 1 for either user in importance. Again, this will only ever be used to compare two users at a time.
I've been pretty unsuccesful in my attempts, but here is what I've tried, just piecing things together:
#attempt one: trying to exclude answers that were not answered by both users
SELECT * FROM user_answers AS uid1
JOIN user_answers AS uid2 ON uid1.uid = uid2.uid
WHERE uid1.uid = 1
AND uid2.uid = 20008
AND uid1.quid IS NOT NULL
AND uid2.quid IS NOT NULL;
This returns no results but I'm not exactly sure why.
#attempt two: trying to exclude where answers are the same for both users
SELECT * FROM user_answers AS uid1
LEFT JOIN user_answers AS uid2 ON (uid1.uid = uid2.uid AND uid1.answer <> uid2.answer)
This gives me results, but seems to be doubling up on everything because of the join. I also tried in this attempt to eliminate any answers what were the same, which seems to be working in that sense.
Any guidance is appreciated.
Thanks.
You can answer your question using an aggregation query. The idea is to using the having clause to filter the rows for the conditions you are looking at.
Because you are not interested at all in questions with importance = 1 those are filtered using a where clause:
select ua.quid
from user_answers ua
where importance <> 1 and uid in (1, 20008)
group by ua.quid
having sum(uid = 1) > 0 and
sum(uid = 20008) > 0;
If you want to include the answers, you can do:
select ua.quid,
group_concat(concat(uid, ':', answer) order by uid) as answers
Just a simple version of what you need.
select *
from user_answers a,
user_answers b
where a.quid = b.quid
and a.uid <> b.uid
and 1 not in (a.importance, b.importance)
If you like to filter just the questions just change the * for distinct a.quid
See it here on fiddle: http://sqlfiddle.com/#!2/15d447/15
Hello I'm having a comments table on which I run a fulltext search.
c1 and c2 are aliases on the same table used
via criteria: c1.parent_id=0 I get the questions only(not the answers attached to them)
and via c2.parent_id<>0 I filter the questions that already have answers
SELECT DISTINCT c1.comment, c1.comment_id, MATCH(c1.comment) AGAINST ('keyword1 keyword2 keyword3') AS score
FROM comments AS c1
JOIN comments AS c2
ON c1.comment_id = c2.parent_id
WHERE c1.parent_id=0
and c2.parent_id <> 0
ORDER BY score DESC LIMIT 9
The problem is that when I run EXPLAIN SELECT... the search looks up through each and every row of the table - so the bigger it gets the slower this operation will be, instead of searching just the rows with parent_id=0.
I would like to ask: is it possible to optimize this kind of query any further?
add an index to all the id columns
alter table your_table add index(parent_id)
same with comment_id
Hi: I need a way to limit the results of a query with SUM or COUNT as if using limit. Is this possible? I have a table with questions and answers. Each entry is identified with "0" if question or "1" if response. Each answer have the questionID, so I can order by this field. I want to show the first 20 questions with all his answers without using limit, because using LIMIT 0,20 would be showing the first 20 entries no matter if they are questions or answers.
I would like to see some logic like this:
SELECT *, SUM(IF(level = '0', 1,0)) AS MyCount FROM table
WHERE MyCount<20 ORDER BY questionID,timestamp
how could I accomplish this? Any suggestion is appreciated.
Since you're doing a GROUP function (sum), try using HAVING instead of WHERE:
SELECT *, SUM(IF(level = '0 ', 1,0)) AS MyCount
FROM table
HAVING MyCount < 20
ORDER BY
questionID,timestamp
-- Edit --
Because you are storing two different types of data (response and question) in the same table, you can try a self join (untested):
SELECT
question.id AS question_id,
question.name AS question_name,
response.id AS response_id,
response.name AS response_name
FROM
table AS question
JOIN
table as response
ON
question.id AND response.level = 1
WHERE
question.level = 0;
this sounds like a badly designed schema. have your questions in one table and your answers in another, make the question ID a foreign key on the answer table. chucking both of them together in the same table is why you now have this problem!