[英]MySQL select row from one table with multiple rows in a second table and get array of multi row in selected row
i have one table containing "Client" information, and another including "Tickets" information for each client.我有一个包含“客户”信息的表格,另一个包含每个客户的“门票”信息。
int-------| varchar -------| varchar
client_id | client_name | client_tickets
----------+----------------+--------------
1 | Title one | 1,2
2 | Title two | 2,3
Simplified tickets table简化票务表
int--------| varchar -------| varchar
ticket_id | ticket_name | ticket_price
-----------+-------------+--------------
1 | ticketone | 30
2 | tickettwo | 40
3 | ticketthree | 50
4 | ticketfour | 60
5 | ticketfive | 70
With the above two tables, I want to produce a single table with a single query with all the pertinent information to generate a search grid So as to give the following output:有了上面的两个表,我想用一个查询生成一个包含所有相关信息的表,以生成一个搜索网格,从而给出以下 output:
client_id | client_name | client_tickets | ticket_names | ticket_prices
----------+----------------+----------------+-----------------------+--
1 | Title one | 1,2 | ticketone,tickettwo | 30,40
2 | Title two | 2,3 | tickettwo,ticketthree | 40,50
ticket_names,ticket_ids,client_name are varchar ticket_names,ticket_ids,client_name 是 varchar
I want to receive the final 5 columns with one request for example:我想通过一个请求接收最后 5 列,例如:
SELECT s.*,
(SELECT GROUP_CONCAT(ticket_name SEPARATOR ',') FROM tickets_table WHERE ticket_id IN(s.client_tickets)) AS ticket_names,
(SELECT GROUP_CONCAT(ticket_price SEPARATOR ',') FROM tickets_table WHERE ticket_id IN(s.client_tickets)) AS ticket_prices
FROM client_table s where s.client_id=1
Which seems to have a problem Do you have a better suggestion?哪个好像有问题 你有更好的建议吗?
Please make your suggestions请提出你的建议
What you want a fairly straightforward SELECT
query with some LEFT/INNER JOIN
(s).你想要一个相当简单的SELECT
查询和一些LEFT/INNER JOIN
(s)。
This website has some good examples/explanations which seem very close to your need: https://www.mysqltutorial.org/mysql-inner-join.aspx这个网站有一些很好的例子/解释似乎非常接近你的需要: https://www.mysqltutorial.org/mysql-inner-join.aspx
I would give you a quick working example, but it is not really clear to me what datatype the relevant columns are.我会给你一个快速工作的例子,但我并不清楚相关列是什么数据类型。 Both tables' _id
-columns are likely some variant of INTEGER, are they also both primary keys (or otherwise atleast indexed?), the client_name
/ ticket_name
are likely VARCHAR/TEXT/STRING types, but how exactly is the remaining column stored?两个表的_id
列可能是 INTEGER 的一些变体,它们也是主键(或者至少是索引?), client_name
/ ticket_name
可能是 VARCHAR/TEXT/STRING 类型,但是剩余列到底是如何存储的? as json or array or?作为 json 或阵列或? (+details) (+细节)
Also you tagged your post with PHP
, are you just after the SQL query?您还用PHP
标记了您的帖子,您是在 SQL 查询之后吗? or looking for PHP code with the SQL inside it.或查找 PHP 代码,其中包含 SQL。
Also, a minor recommendation.另外,一个小建议。 If you're just starting out making whatever you're building, and aren't married to these specifics yet.如果您刚刚开始制作您正在构建的任何东西,并且还没有与这些细节结婚。
If you're using an array or json to store those client_tickets
, and you're not ALSO using that JSON to store a ton of other info that you've left out here, then i would recommend not using json for this.如果您使用数组或 json 来存储这些client_tickets
,并且您也没有使用该 JSON 来存储您在此处遗漏的大量其他信息,那么我建议您不要使用 Z466DEEC76ECDF24D546D
A simple junction table would be a far more natural way to store such relationships, and easier to manage with SQL.一个简单的联结表将是存储此类关系的一种更自然的方式,并且使用 SQL 更易于管理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.