简体   繁体   English

从多个表中为每个用户选择数据

[英]Selecting data for each user from multiple tables

I'm trying to add a function to a script that orders a list of users and then allows that same order SQL to be used to create an export file. 我正在尝试向脚本中添加一个函数,该脚本对用户列表进行排序,然后允许使用相同顺序的SQL创建导出文件。 I'm writing it for a piece of software that hosts basic user data in one table, and the question value I'm trying to get in another. 我正在为一款将基本用户数据托管在一个表中的软件编写代码,并尝试在另一个表中获取问题值。 Here's the details: 详细信息如下:

Table1 is base_user and from it I need the values of columns id , username , and email . 表1是base_user ,从中我需要idusernameemail列的值。 However, I want to add the option to order/export by users with all that same data, but by their sex. 但是,我想为所有具有相同数据但性别不同的用户添加用于订购/导出的选项。 Table2 is base_question_data and from it I want to get the question 'sex' value. 表2是base_question_data ,我想从中获取问题的“性别”值。

Users can pick between Male or Female (Values: 1 or 2), and that info is stored in a column named intValue . 用户可以在“男性”或“女性”之间进行选择(值:1或2),并且该信息存储在名为intValue的列中。 I've already tried using INNER JOINS and such selecting multiple info from both tables, but where I'm getting confused is how to say "Get id, username, email, and sex for every user that is X sex" where "X sex" is the gender the user set to order/export by. 我已经尝试过使用INNER JOINS并从两个表中选择多个信息,但是让我感到困惑的是如何说“为每个X性别的用户获取ID,用户名,电子邮件和性别”,其中“ X性别” “是用户设置用于订购/导出的性别。 I'm having a hard time figuring out how to get the specifics for the sex value for each user, but also use it to only show all those users of that value. 我很难弄清楚如何获取每个用户的性别值的详细信息,但也很难用它来显示所有具有该值的用户。 All ideas are appreciated. 所有的想法表示赞赏。

EDIT: Forgot to mention that in the 'base_question_data' table, column 'userId' is equal to column 'id' in 'base_user' table. 编辑:忘记提及“ base_question_data”表中的列“ userId”等于“ base_user”表中的列“ id”。

这是Table1或base_user This is Table1 or base_user 这是Table1或base_user 这是Table2或base_question_data This is Table2 or base_question_data 这是Table2或base_question_data

To clarify what I'm trying to do: In the 'base_user' table, I want to select ID, Username, and Email. 要阐明我要执行的操作:在“ base_user”表中,我要选择ID,用户名和电子邮件。 I have this working normally, as it's a simple query. 我的这个工作正常,因为它是一个简单的查询。 I want to, however, let users order by each user's gender. 但是,我想让用户按每个用户的性别排序。 So 1)They can order the preview list by Male (questionName = sex intValue = 1) or Female (questionName = sex intValue = 2). 因此1)他们可以按男性(questionName = sex intValue = 1)或女性(questionName = sex intValue = 2)排序预览列表。 This way, it will show all user's ID, Username, and Email who are gender 1 or 2. Using this same query, I'm trying to let them export that data as well, so they can export only users of gender 1 or 2. 这样,它将显示所有性别为1或2的用户ID,用户名和电子邮件。使用同一查询,我试图让他们也导出该数据,因此他们只能导出性别为1或2的用户。 。

My problem is the process of combining all of this data. 我的问题是合并所有这些数据的过程。 I need to combine base_user's "id" to base_question_data's "userId" and I need to also get the value of each user by targeting base_question_data's questionName='sex' and get the intValue based on if they're ordering by Male (value=1) or Female (value=2). 我需要将base_user的“ id”与base_question_data的“ userId”组合起来,我还需要通过定位base_question_data的questionName =“ sex”来获取每个用户的价值,并根据他们是否按男性(值= 1)进行排序来获取intValue。或女性(值= 2)。

I've done LEFT JOINS when combining one value to another for two tables, and while this is still two tables, I've never done it where I need to combine two different keys from two tables while also ordering them all by two different column values in one of those tables. 当将两个表的一个值与另一个值组合时,我已经完成了LEFT JOINS,尽管这仍然是两个表,但我从未在需要将两个表中的两个不同键组合在一起的同时,还按两个不同的列对它们进行排序这些表之一中的值。

我同意@rowmoin,但是如果您使用LEFT JOIN,如果base_question_data表中没有任何条目,它将提供null值;如果您使用INNER JOIN,它将忽略两个表中不匹配的记录,因此,如果您在base_question_data表中没有相关条目。

The query was easier than I anticipated. 查询比我预期的要容易。 I will explain it in detail here. 我将在这里详细解释。

To achieve something like this, (in which I needed one table's two values to decide what I'm getting in my other table results), I simply changed the value of 'intValue' in my query, but you can also do this by assigning a php value to let it be dynamically changed if you want. 为了达到这样的目的(在其中我需要一个表的两个值来确定我在其他表结果中得到的值),我只是在查询中更改了'intValue'的值,但是您也可以通过分配一个php值,以便根据需要动态更改。 Since I'm doing a change between 1 or 2, I just have to different queries with the respective variable. 由于我要在1或2之间进行更改,因此只需要使用相应的变量进行不同的查询即可。

Here's my query: 这是我的查询:

SELECT * FROM ow_base_user
                INNER JOIN ow_base_question_data ON ow_base_user.id = ow_base_question_data.userId
                WHERE questionName='sex' AND intValue='$value';

I finally realized this morning I needed to go backwards, rather, and select my conditionals from base_question, rather than from the base_user table. 我终于意识到今天早上我需要倒退,而是从base_question而不是从base_user表中选择条件。 Using this query, (where $value=1 for male or $value=2 for Female) I have successfully gotten to return a list of user's who match the set $value with their IDs, Username, and Email. 使用此查询(男性为$value=1 ,女性$value=2 ),我成功地返回了一个用户列表,该列表匹配设置的$ value及其ID,用户名和电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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