MySQL查询,我该如何查询?

时间:2016-06-07 18:45:37

标签: php mysql wordpress

我有一个需要加入2个表的查询。

活动表:

activity.id: 334
activity.user_id: 14
activity.type: quiz_evaluated
activity.item_id: 2968
activity.secondary_item_id: 3006
activity.date_recorded: 2016-05-25 15:07:14

Postmeta表:

postmeta.meta_id: 7856
postmeta.post_id: 3006
postmeta.meta_key: quiz_questions14
postmeta.meta_value: a:2:{s:4:"ques";a:4:{i:0;s:4:"2972";i:1;s:4:"2974"..

这就是我需要的:

SELECT activity.item_id as course_id,
  postmeta.meta_value as quiz_data
FROM wp_bp_activity as activity
JOIN wp_postmeta as postmeta
  ON postmeta.post_id = activity.secondary_item_id
WHERE activity.type = 'quiz_evaluated'
  AND 'quiz_questions'+activity.user_id = postmeta.meta_key

但问题是,postmeta.post_id不是唯一的,我需要确认2个变量,postmeta.post_idpostmeta.meta_key(即'quiz_question'+ activity.user_id)才能获得数据库中的一行。

我怎么能实现这个目标?

PS:我目前正在WordPress中执行此操作

1 个答案:

答案 0 :(得分:2)

使用concat

transition