douchuanhan8384 2013-12-10 17:18
浏览 33
已采纳

MySQL:您可以根据第三个表中字段的值选择要选择的表吗?

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?

  • 写回答

2条回答 默认 最新

  • dongyingming8970 2013-12-10 17:40
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。