循环遍历字符串的每个值

时间:2016-04-26 05:48:23

标签: php mysql sql

我得到表单db字段(organisations.paths)以下字符串:

  

/ 1/2/3/4

每个数字都是此数据库表中组织名称的ID:

+----+-------------+----------+----------+----------------------+
| id | frameworkid |   path   | parentid |       fullname       |
+----+-------------+----------+----------+----------------------+
|  1 |           1 | /1       |        0 | NYC University       |
|  2 |           1 | /2       |        0 | Board of directors   |
|  3 |           1 | /1/2/3   |        1 | Math faculty         |
|  4 |           1 | /1/2/3/4 |        3 | Statistic department |
|  5 |           1 | /1/2/3/5 |        2 | Linguist  department |
+----+-------------+----------+----------+----------------------+

然后我为每个组织提供description table

+----+----------+---------+----------------+
| id |   data   | fieldid | organisationid |
+----+----------+---------+----------------+
|  1 | HQ       |       1 |              1 |
|  2 | advisory |       1 |              2 |
|  3 | advisory |       1 |              3 |
|  4 | bottom   |       1 |              4 |
|  5 | advisory |       1 |              5 |
+----+----------+---------+----------------+

如何join两个说明表和主表并仅通过其说明中包含HQadvisory的组织进行循环播放?所以它变成了:

  

纽约大学,董事会,数学系(统计部门 - 不会显示,因为它与描述bottom

1 个答案:

答案 0 :(得分:1)

您需要使用PHP和mysql的# assuming that @user.accounts returns all accounts user_accounts = @user.accounts.group_by(&:name) # querying the hash. use `first` to receive the first matching # account (even if there is only one) user_accounts['nameofproduct'].first explodeIN函数。

join

制作循环以获取名称并根据需要显示它们。