3

我需要将成员从 ExpressionEngine 移动到 WordPress。到目前为止,我在 Stackoverflow 上得到了以下查询的帮助,该查询效果很好并产生了 696 个结果(正确的成员数量)。但是,我还需要从另一个表中导出一些列,当我将它们添加到查询中时,我得到 484416 个结果(696 x 696)。

这是返回 696 个结果的原始查询:

SELECT username AS user_login, 
username AS user_nicename, 
email AS user_email, 
url AS user_url, 
screen_name AS display_name, 
FROM_UNIXTIME(join_date) AS user_registered
FROM exp_members

这是我想做的,我尝试用Navicat的查询生成器来做。m_field_id_1从表中获取调用的列exp_member_data并将其返回为user_location. 此查询产生 484416 个结果。

SELECT exp_members.username AS user_login, 
exp_members.username AS user_nicename, 
exp_members.email AS user_email, 
exp_members.url AS user_url, 
exp_members.screen_name AS display_name, 
FROM_UNIXTIME(join_date) AS user_registered, 
exp_member_data.m_field_id_1 AS user_location // here is the m_field_id_1
FROM exp_members, exp_member_data

如何正确组合这 2 个表,以便仍然只有 696 个返回结果但带有附加列?谢谢

4

1 回答 1

2

您需要JOIN表格之间的条件,或者您得到两者的笛卡尔积(table1 的所有行乘以 table2 的所有行,在这种情况下为 696^2)。

SELECT
  exp_members.username AS user_login, 
  exp_members.username AS user_nicename, 
  exp_members.email AS user_email, 
  exp_members.url AS user_url, 
  exp_members.screen_name AS display_name, 
  FROM_UNIXTIME(join_date) AS user_registered, 
  exp_member_data.m_field_id_1 AS user_location
FROM
  exp_members
  JOIN exp_member_data 
    /* ON clause specifies the relation between the two tables */
    ON exp_members.username = exp_member_data.username

以上假设在exp_member_data称为username映射到的列中有一个exp_member.username。如果它们之间有一个 id 列,请使用它,如下所示:

  JOIN exp_member_data ON exp_members.member_id = exp_member_data.member_id
于 2012-04-19T01:57:45.557 回答