简体   繁体   English

如何在单独的表中显示与 ID where user = x WHEN 对应的所有 MYSQL 表值

[英]How to display all MYSQL table values corresponding to ID where user = x WHEN in separate tables

I would still classify myself as a beginner in PHP and mysqli databases and I could use some help.我仍然将自己归类为 PHP 和 mysqli 数据库的初学者,我可以使用一些帮助。 I have looked around on the site and I could not find a clear answer.我在网站上环顾四周,找不到明确的答案。

I currently have a table of 150 athletes which each have been assigned an Auto Incremented ID (column one 'athlete_id')and some stats for each athlete in additional columns which have been set up like such: MySQL 'athletes' table我目前有一个包含 150 名运动员的表格,每个运动员都被分配了一个自动增量 ID(第一列“athlete_id”)和每个运动员的一些统计数据,这些统计数据位于附加列中,这些列的设置如下: MySQL 'athletes' 表

Next, I have another table within the same database named 'selection'(image here) in the selection table there is an id column which gathers the logged in users id from my session and places the users_id into column 1 of id in 'selection'.接下来,我在选择表中的同一个数据库中有另一个名为“选择”(此处图像)的表,有一个 id 列,它从我的会话中收集登录的用户 ID,并将 users_id 放入“选择”中 id 的第 1 列. The remaining columns are the id's of athletes (athlete_id) in which the logged in user has selected and have been inserted into the table.其余的列是运动员的 id (athlete_id),登录用户在其中选择并插入到表中。 Id's of athlete1, athlete2 etc. correspond to the athlete_id in "athletes" (table 1) athlete1、athlete2 等的 Id 对应于“athletes”中的 athlete_id(表 1)

What I am attempting to do is perhaps select the users_id from the session (which I have done and for this example I use '21' as 'user_id' in session) then grab the athletes_id the user has selected that are in the 'selection' table under columns athlete1 athlete 2 etc. then replace the athlete id with real name and statistics which are in the 'athletes table'我试图做的可能是从会话中选择 users_id(我已经完成了,对于这个例子,我在会话中使用 '21' 作为 'user_id')然后获取用户在'选择'中选择的运动员 ID athlete1 athlete 2 等列下的表格,然后将运动员 ID 替换为“运动员表格”中的真实姓名和统计信息

Below I have joined the tables but under this way it only shows athlete3 that user 21 selected and I am unsure of how to show all selections of user 21 in an effective way.下面我加入了表格,但在这种方式下,它只显示用户 21 选择的 athlete3,我不确定如何以有效的方式显示用户 21 的所有选择。 How would i be able to show all athletes selected from user 21?我如何才能显示从用户 21 中选择的所有运动员?

SELECT * FROM athletes LEFT JOIN selection ON athletes.athlete_id=selection.athlete3 WHERE id=21

In the future, I will be using this info to show the selected athletes along with the athletes stats to the logged in user via an HTML table on a website.将来,我将使用此信息通过网站上的 HTML 表格向登录用户显示选定的运动员以及运动员的统计数据。 I have made this example to show my end goal for this.我做了这个例子来展示我的最终目标。

The way I have gone about storing each athlete selection in separate columns (athlete1, athlete2, athlete3) may be EXTREMELY inefficient in my case and if there is another much simpler way to achieve the same result I would very much appreciate the tip and I could change my entire approach to this problem.我将每个运动员选择存储在单独的列(运动员 1、运动员 2、运动员 3)中的方式在我的情况下可能非常低效,如果有另一种更简单的方法来实现相同的结果,我将非常感谢提示,我可以改变我解决这个问题的整个方法。

I have considered storing the selected athlete_id in one column with commas ex: 4,8,9 but im unsure how to do this so if that is an easier solution I could use some help from someone more experienced.我已经考虑将选定的 athlete_id 存储在一个带有逗号的列中,例如:4,8,9,但我不确定如何执行此操作,因此如果这是一个更简单的解决方案,我可以从更有经验的人那里获得一些帮助。

I will try to solve your problem.我会尽力解决你的问题。 If you want to do a join query you must have the same column as the other table columns.如果要执行连接查询,则必须具有与其他表列相同的列。

As in your case, you must have 1 column in the selection table which is the same as the athletes table's athlete_id .与您的情况一样, selection表中必须有 1 列,这与athletes表的athlete_id So the query that you will generate.所以您将生成的查询。

SELECT * FROM athletes 
LEFT JOIN selection ON athletes.athlete_id = selection.athlete_id 
WHERE selection.id = 21;

Hopefully, this can solve your problem.希望这可以解决您的问题。

暂无
暂无

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

相关问题 MYSQL查询:必须返回表“ B”中的user = jack的所有记录,并带有相应的颜色匹配“ photo_id” - MYSQL query: All records where user=jack from table `B` must be returned with the corresponding color matching `photo_id` MYSQL-选择表A中的值,其中表B中的所有相应值都具有特定值 - MYSQL - Select values in table A where all the corresponding values in table B have a specific values 如何显示来自MySQL数据库的两个单独表中的值? - How to display values from two separate tables from mySQL database? Mysql返回表的所有值,其中列等于用户输入 - Mysql Return all values of a table where column is equal to user input 当用户输入ID时,在mysql表中显示行 - Display row in mysql table when a user enter id MySQL如何从表A中选择表B中所有对应项都满足条件的项 - mysql how to select items from Table A where all corresponding items in Table B satisfy condition 如何在两个单独的表中使用mysql WHERE IN - How to use mysql WHERE IN for two separate tables 如何使用php将mysql数据库中的所有表显示或显示到html表中 - How to display or show all tables in a mysql database into html table with php MYSQL:在一张表中搜索用户ID以在其他2张表中搜索数据,然后显示所有3张表中的数据 - MYSQL: Search for User ID in one table to search for data in 2 other tables, then show data from all 3 tables 如何获取用户ID不在表记录中的所选行mysql - How get selected rows where user id is not in table record mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM