如何离开乔?

左桌子

SELECT TYPE , days
FROM leavetypes


type  days
anual   10

casual  6

sick    10


右表

SELECT sub_leave_type, (
SUM( working_days )
) AS used
FROM vacations
WHERE `user_id` = '1'
AND `from_date`
BETWEEN '2012-01-01'
AND '2012-12-31'
GROUP BY sub_leave_type


sub_leave_type      used

anual               3

casual              6


实际上,我在cakephp上执行此操作,这是我的代码。

$allleaves = $this->Leavetype->find('all', array(
        'joins' => array(
            array(
                'table' => 'vacations',
                'alias' => 'vacationsJoin',
                'type' => 'left',
                'conditions' => array('vacationsJoin.sub_leave_type = Leavetype.type')
            )
        ),
        'conditions' => array(
            'vacationsJoin.user_id' => $_SESSION['Auth']['User']['id'],
            'vacationsJoin.from_date  BETWEEN ? and ?' => array(date('Y') . '-01-01',
                date('Y') . '-12-31')
        ),
        'fields' => array('type', 'days', '(SUM(working_days)) as used'),
        'group' => 'sub_leave_type',
            ));

    $this->set(compact('allleaves'));


这并不是我想要的,我想要的是左表的所有行以及假期表中的其他行

最佳答案

请阅读此查询并根据您的要求进行更改

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

关于mysql - 左联接(mysql),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9564615/

10-11 05:25