我见过其他类似的问题,但没有一个对我想做的事情真正有帮助。
我有三张桌子;sites
,user_sites
和site_domains
. 我想从user_sites
和site_domains
表中查询一个站点以及用户和域的列表。以下是表格示例:
数据库站点
+------+-------------+---------------------+--------------+
| id | site_name | site_options | created_on |
+------+-------------+---------------------+--------------+
| 1 | name1 | {serialized data} | 10/10/12 |
+------+-------------+---------------------+--------------+
| 2 | name2 | {serialized data} | 24/10/12 |
+------+-------------+---------------------+--------------+
| 3 | name3 | {serialized data} | 08/09/12 |
+------+-------------+---------------------+--------------+
| 4 | name4 | {serialized data} | 03/01/12 |
+------+-------------+---------------------+--------------+
| 5 | name5 | {serialized data} | 10/12/11 |
+------+-------------+---------------------+--------------+
db.user_sites
+------+-----------+-----------+-----------+
| id | user_id | site_id | default |
+------+-----------+-----------+-----------+
| 1 | 1 | 1 | 0 |
+------+-----------+-----------+-----------+
| 2 | 3 | 2 | 0 |
+------+-----------+-----------+-----------+
| 3 | 1 | 3 | 0 |
+------+-----------+-----------+-----------+
| 4 | 10 | 1 | 0 |
+------+-----------+-----------+-----------+
| 5 | 5 | 1 | 1 |
+------+-----------+-----------+-----------+
db.site_domains
+------+-----------+-------------------+
| id | site_id | site_domain |
+------+-----------+-------------------+
| 1 | 1 | www.domain1.com |
+------+-----------+-------------------+
| 2 | 1 | sub.domain2.com |
+------+-----------+-------------------+
| 3 | 3 | www.domain3.com |
+------+-----------+-------------------+
| 4 | 2 | www.domain4.com |
+------+-----------+-------------------+
| 5 | 3 | www.domain5.com |
+------+-----------+-------------------+
当它完成时。我希望能够填充这样的对象:
array (
[1] => array (
[site_name] => 'Name1',
[created_on] => '10/10/12',
[site_options] => {serialized data},
[domains] => array (
[0] => 'www.domain1.com',
[1] => 'www.domain2.com'
),
[users] => array (
[0] => 1,
[1] => 10,
[2] => 5
)
),
[2] => array (
[site_name] => 'Name2',
[created_on] => '24/10/12',
[site_options] => {serialized data},
[domains] => array (
[0] => 'www.domain4.com'
),
[users] => array (
[0] => 3
)
)
)
....等等。
我正在使用 Codeigniter 创建我的应用程序,但如果我只知道它的 SQL,我可以将其转换为使用 Codeigniter。SQL 不是我最适合的,但任何帮助将不胜感激。