简体   繁体   中英

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. 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 . 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.

Users can pick between Male or Female (Values: 1 or 2), and that info is stored in a column named 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. 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.

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

To clarify what I'm trying to do: In the 'base_user' table, I want to select ID, Username, and Email. 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). 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.

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).

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.

我同意@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. Since I'm doing a change between 1 or 2, I just have to different queries with the respective variable.

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. 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.

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