【问题标题】:Selecting multiple tables and unions in Laravel在 Laravel 中选择多个表和并集
【发布时间】:2016-06-23 15:09:00
【问题描述】:

我正在 Laravel 中做一个项目,该项目是对旧版 Vanilla PHP 项目的更新,我遇到了一个在 Laravel 中无法完全复制的查询。

我需要一些帮助。这个想法只是简单地计算查询中提到的与用户提供的电子邮件匹配的任何表中的所有电子邮件。此外,根据各自的表格,电子邮件创建日期应大于或等于两天。

$sqlDuplicates = "
    select count(*) as total from (
        select distinct(email) from clientes where created_date>='".$data1."' and email = :txEmail
        union all (
        select distinct(txEmailC2L) from records_SegurosLogoC2L where dtCreated>='".$data1."' and txEmailC2L = :txEmail
        )
        union all (
        select distinct(txEmailC2L) from records_SimuleSegurosLogoC2L where dtCreated>='".$data1."' and txEmailC2L = :txEmail
        )
        union all (
        select distinct(email) from records_LogoOnline where created_date>='".$data1."' and email = :txEmail
        )
        union all (
        select distinct(email) from records_SegurosAutoEconomico where created_date>='".$data1."' and email = :txEmail
        )
        union all (
        select distinct(email) from records_SimuleSegurosLogo where created_date>='".$data1."' and email = :txEmail
        )
    ) as data
";

这就是查询。如果有人能提供帮助,我将不胜感激

【问题讨论】:

标签: php mysql laravel eloquent


【解决方案1】:

Laravel 5 中正确的语法是:

    $sqlDuplicates = DB::table('f_table_name')
                    ->join('s_table_name', 'f_table_name.id', '=', 's_table_name.photo_id')
                    ->where('created_date', '>=', $date)
                    ->orderBy('created_at', 'desc')
                    ->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2020-01-01
    • 2015-12-21
    相关资源
    最近更新 更多