简体   繁体   中英

MySQL: Can you choose which table to select from based on the value of a field in a third table?

I have 2 tables (activities and questions).

Activities Table:

+-------------+------------+------------+
| activity_id | title      | flow       |
+-------------+------------+------------+
| 1           | Activity 1 | question-2 |
+-------------+------------+------------+
| 2           | Activity 2 | activity-3 |
+-------------+------------+------------+

Questions Table:

+-------------+-----------------+------------+
| question_id | question        | flow       |
+-------------+-----------------+------------+
| 1           | Lorem ipsum...? | activity-1 |
+-------------+-----------------+------------+
| 2           | Lorem ipsum...? | question-3 |
+-------------+-----------------+------------+
| 3           | Lorem ipsum...? | activity-2 |
+-------------+-----------------+------------+

What I am trying to do, is if I want to look up and see where a user flows after a certain question or activity, then look up that question or activities data, all in one query, how would I do that?

Is there a way in MySQL to analyze the flow field and pull data from the corresponding table and id.

So for instance, if I know a user is on question 2 (question_id: 2) and I want to look up the title/question and flow fields of the item that comes after question 2 (which is question 3 in this case (flow: question-3)), how would I do this in one query.

I hope this is making since. Can I tell MySQL that if it finds activity-1 in the flow field, then it needs to look up the information for activity 1 in the activities table. If it finds question-1 in the flow field, then look up the information for question 1 in the questions table.

If possible, is a complex query like this better than running 2 queries from PHP PDO? Would it be more optimized to look up the flow field, then analyze it in PHP and have a second query to grab the flow fields activity or question data?

If you're currently on a question, this will return where you go next:

SELECT q.flow next, a.title text, a.flow
FROM activities a
JOIN questions q ON CONCAT('activity-', a.activity_id) = q.flow
WHERE q.question_id = $current_question
UNION ALL
SELECT q2.flow next, q1.question text, q1.flow
FROM questions q1
JOIN questions q2 ON CONCAT('question-', q1.question_id) = q2.flow
WHERE q.question_id = $current_question

DEMO

If you're currently on an activity, you swap all questions and activities:

SELECT a.flow next, q.question text, q.flow
FROM questions q
JOIN activities a ON CONCAT('question-', q.question_id) = a.flow
WHERE a.activity_id = $current_activity
UNION ALL
SELECT a2.flow next, a1.title text, a2.flow
FROM activities a1
JOIN activities a2 ON CONCAT('activity-', a1.activity_id) = a2.flow
WHERE a.activity_id = $current_activity

You could use a UNION SELECT

SELECT 'activity-' + activity_id as id, title, flow FROM activities
UNION SELECT 'question-' + question_id, question, flow FROM questions

and using flow to refer to id in the same (UNION) table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM