gpt4 book ai didi

sql - Zend 框架 : How to combine three tables in one query using Joins?

转载 作者:太空狗 更新时间:2023-10-30 01:50:18 24 4
gpt4 key购买 nike

我有三个这样的表:

人员表:

person_id |    name     |   dob
--------------------------------
1 | Naveed | 1988
2 | Ali | 1985
3 | Khan | 1987
4 | Rizwan | 1984

地址表:

address_id |  street  |   city  |  state  | country
----------------------------------------------------
1 | MAJ Road | Karachi | Sindh | Pakistan
2 | ABC Road | Multan | Punjab | Pakistan
3 | XYZ Road | Riyadh | SA | SA

Person_Address 表:

person_id | address_id
----------------------
1 | 1
2 | 2
3 | 3

现在我想通过一个查询获取 Person_Address 表的所有记录以及他们的个人和地址记录:

person_id|    name  |  dob  | address_id |  street  |   city  |  state  | country
----------------------------------------------------------------------------------
1 | Naveed | 1988 | 1 | MAJ Road | Karachi | Sindh | Pakistan
2 | Ali | 1985 | 2 | ABC Road | Multan | Punjab | Pakistan
3 | Khan | 1987 | 3 | XYZ Road | Riyadh | SA | SA

如何使用 zend?谢谢

最佳答案

引用指南是了解Zend_Db_Select的最佳起点.当然还有我下面的例子:

//$db is an instance of Zend_Db_Adapter_Abstract
$select = $db->select();
$select->from(array('p' => 'person'), array('person_id', 'name', 'dob'))
->join(array('pa' => 'Person_Address'), 'pa.person_id = p.person_id', array())
->join(array('a' => 'Address'), 'a.address_id = pa.address_id', array('address_id', 'street', 'city', 'state', 'country'));

获取一行就这么简单:

$db->fetchRow($select);

在调试 Zend_Db_Select 时,您可以使用一个巧妙的技巧 - 简单地打印 select 对象,然后调用 toString 方法来生成 SQl:

echo $select; //prints SQL

关于sql - Zend 框架 : How to combine three tables in one query using Joins?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2354814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com