如何将其写为SQL查询

时间:2014-03-17 11:46:21

标签: mysql sql

我有一个用php编写的代码及其查询,但我想转换成sql查询

return TimetableEntry::select('timetable_entries.id as tid')

    ->addSelect('timetable_entries.id as tid')

    ->join('weekdays', 'timetable_entries.weekday_id', '=', 'weekdays.id')

    ->addSelect('weekdays.weekday as tweek')

    ->join('employees', 'timetable_entries.employee_id', '=', 'employees.id')

    ->addSelect('employees.id as eid')

    ->join('users', 'users.username', '=', 'employees.employee_number')

    ->join('timetables', 'timetables.id', '=', timetable_entries.timetable_id')

    ->addSelect('timetables.start_date as tstart')

    ->addSelect('timetables.end_date as tend')

    ->join('class_timings', 'class_timings.id', '=','timetable_entries.class_timing_id')

    ->addSelect('class_timings.start_time as start')

    ->where('users.id','=',$this->id)

    ->get();

4 个答案:

答案 0 :(得分:0)

查询可能看起来像

select timetable_entries.id as tid ,
weekdays.weekday as tweek,
employees.id as eid,
timetables.start_date as tstart,
timetables.end_date as tend,
class_timings.start_time as start

From timetable_entries
join weekdays
ON
timetable_entries.weekday_id=weekdays.id

join employees
ON 
timetable_entries.employee_id=employees.id

join users
ON users.username=employees.employee_number

join timetables
ON timetables.id=timetable_entries.timetable_id

join class_timings
ON class_timings.id=timetable_entries.class_timing_id

where users.id=timetable_entries.id

答案 1 :(得分:0)

这应该让你开始:显然没有优化。

SELECT
    timetable_entries.id AS tid,
    weekdays.weekday AS tweek,
    employees.id AS eid
    timetables.start_date as tstart
    timetables.end_date as tend
    class_timings.start_time as start
FROM
    timetable_entries,
    weekdays,
    employees,
    timetables,
    class_timings
WHERE
    timetable_entries.weekday_id = weekdays.id,
    timetable_entries.employee_id = employees.id,
    users.username = employees.employee_number,
    timetables.id = timetable_entries.timetable_id,
    class_timings.id = timetable_entries.class_timing_id,
    users.id = <PUT_ID_HERE>

答案 2 :(得分:0)

只要按照SELECT语法

的顺序重写这样的查询就不算太复杂了
-- First just add all select/addselects

SELECT timetable_entries.id as tid,
       weekdays.weekday as tweek,
       employees.id as eid,  
       timetables.start_date as tstart,
       timetables.end_date as tend,
       class_timings.start_time as start

-- then add all the JOINs

FROM timetable_entries
JOIN weekdays ON timetable_entries.weekday_id = weekdays.id
JOIN employees ON timetable_entries.employee_id = employees.id
JOIN users ON users.username = employees.employee_number
JOIN timetables ON timetables.id = timetable_entries.timetable_id
JOIN class_timings ON class_timings.id = timetable_entries.class_timing_id

-- last, add the WHERE

WHERE users.id = $this->id

答案 3 :(得分:0)

您的代码如下:

SELECT(
    timetable_entries.id AS tid,
    timetables.start_date AS tstart,
    weekdays.weekday AS tweek,
    timetables.end_date AS tend,
    employees.id AS eid,
    class_timings.start_time AS START

    FROM
    timetable_entries
    JOIN weekdays timetable_entries.weekday_id=weekdays.id
    JOIN employees timetable_entries.employee_id=employees.id
    JOIN users users.username=employees.employee_number
    JOIN timetables timetables.id=timetable_entries.timetable_id
    JOIN class_timings class_timings.id=timetable_entries.class_timing_id
    WHERE users.id=?(id);

注意:?标记应该用于前端输入...