MYSQL LEFT 使用“LIKE”连接

发布于 2024-12-08 13:10:40 字数 829 浏览 0 评论 0原文

我正在尝试加入两个表。问题是其中一个表的单元格内实际上有一个以逗号分隔的列表。我需要加入“csv 单元格”。使用我的基于沙拉的示例,我希望培根培根,火鸡一起加入,并给我一个cobb_salad

   SELECT tbl_a.item, tbl_b.item, tbl_b.salad 
     FROM tbl_a
LEFT JOIN tbl_b
       ON  tbl_a.item LIKE CONCAT('%', tbl_b.item, '%')

我尝试了上面的示例和其他一些变体。我意识到这不会是一个有效的查询。问题是底层数据集早于我。幸运的是,我只需要运行查询一次。

Expected Result
+--------+------------+
| item   |   salad    |
+--------+------------|
|  ham   | chef_salad |
| bacon  | cobb_salad |
| turkey | cobb_salad |
+---------------------+


tbl_a 
+------+
| item |
+------+
|bacon | 
| ham  | 
|turkey|
+------+  

tbl_b
+--------------+------------+
|     item     |  salad     |
+--------------+------------+
| ham          | chef_salad |
| bacon,turkey | cobb_salad |
+--------------|------------+

I am trying to join two tables. The issue is one of the tables actually has a comma-separated list inside the cell. I need to join on the 'csv cell.' Using my salad based example I want bacon to join with bacon,turkey and give me a cobb_salad

   SELECT tbl_a.item, tbl_b.item, tbl_b.salad 
     FROM tbl_a
LEFT JOIN tbl_b
       ON  tbl_a.item LIKE CONCAT('%', tbl_b.item, '%')

I tried the example above and a few other varations. I realize this is not going to be an efficent query. The issue is that the underlying dataset predates me. Luckily I only need to run the query once.

Expected Result
+--------+------------+
| item   |   salad    |
+--------+------------|
|  ham   | chef_salad |
| bacon  | cobb_salad |
| turkey | cobb_salad |
+---------------------+


tbl_a 
+------+
| item |
+------+
|bacon | 
| ham  | 
|turkey|
+------+  

tbl_b
+--------------+------------+
|     item     |  salad     |
+--------------+------------+
| ham          | chef_salad |
| bacon,turkey | cobb_salad |
+--------------|------------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2024-12-15 13:10:40

注意:

我在您的数据库中显示,您的表中有许多空格字符
请在插入数据库之前尝试修剪它。
例如:
来自:
火腿 => [空格]火腿[空格]
致:
火腿 => [无空格]火腿[无空格]
希望你明白我说的话。


您可以尝试:
它对我有用:

mysql> use test;
Database changed
mysql> SELECT tbl_a.item as item_a, tbl_b.item as item_b, tbl_b.salad
    -> FROM tbl_a LEFT JOIN tbl_b 
    -> ON FIND_IN_SET(tbl_a.item, tbl_b.item);
+--------+--------------+------------+
| item_a | item_b       | salad      |
+--------+--------------+------------+
| bacon  | bacon,turkey | cobb_salad |
| ham    | ham          | chef_salad |
+--------+--------------+------------+
2 rows in set (0.00 sec)

更新:

这是我的表格:

mysql> select * from tbl_a;
+-------+
| item  |
+-------+
| bacon |
| ham   |
+-------+

并且

mysql> select * from tbl_b;
+--------------+------------+
| item         | salad      |
+--------------+------------+
| bacon,turkey | cobb_salad |
| ham          | chef_salad |
+--------------+------------+

NOTICE:

I show in your db, there are many space characters in your tables.
Please try to trim it before inserting into your db.
Ex:
From:
ham => [space]ham[space]
To:
ham => [no-space]ham[no-space]
Hope you understand what I said.


You can try:
It works for me:

mysql> use test;
Database changed
mysql> SELECT tbl_a.item as item_a, tbl_b.item as item_b, tbl_b.salad
    -> FROM tbl_a LEFT JOIN tbl_b 
    -> ON FIND_IN_SET(tbl_a.item, tbl_b.item);
+--------+--------------+------------+
| item_a | item_b       | salad      |
+--------+--------------+------------+
| bacon  | bacon,turkey | cobb_salad |
| ham    | ham          | chef_salad |
+--------+--------------+------------+
2 rows in set (0.00 sec)

UPDATE:

Here is my tables:

mysql> select * from tbl_a;
+-------+
| item  |
+-------+
| bacon |
| ham   |
+-------+

AND

mysql> select * from tbl_b;
+--------------+------------+
| item         | salad      |
+--------------+------------+
| bacon,turkey | cobb_salad |
| ham          | chef_salad |
+--------------+------------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文