如何使用Laravel执行子查询

时间:2017-01-14 21:34:50

标签: php mysql laravel subquery

我通过此查询获取所有产品在线商店,我想在Laravel 5.3中使用它

因为它是嵌套查询,所以我无法理解它。

如何通过Laravel函数使用此查询:

SELECT 
        pe.entity_id AS eid,
        tbl.attribute_id AS attr_id,
        tbl.attribute_code AS attr_code,
        tbl.`value`
FROM tdshop_product_category AS pc 
INNER JOIN tdshop_product_entity  AS pe ON ( pc.product_id = pe.entity_id AND pe.deleted_at IS NULL)
LEFT JOIN (
        SELECT 
            att.attribute_id,
            att.attribute_code,
          CASE att.backend_type
           WHEN 'varchar' THEN pe_varchar.`value`
           WHEN 'int' THEN pe_int.`value`
           WHEN 'text' THEN pe_text.`value`
           WHEN 'decimal' THEN pe_decimal.`value`
           WHEN 'datetime' THEN pe_datetime.`value`
           ELSE att.backend_type
          END AS `value`,
          CASE att.backend_type
           WHEN 'varchar' THEN pe_varchar.`entity_id`
           WHEN 'int' THEN pe_int.`entity_id`
           WHEN 'text' THEN pe_text.`entity_id`
           WHEN 'decimal' THEN pe_decimal.`entity_id`
           WHEN 'datetime' THEN pe_datetime.`entity_id`
           ELSE att.backend_type
          END AS `entity_id`
        FROM tdshop_entity_attribute AS ea 
        LEFT JOIN tdshop_attribute AS att ON (att.attribute_id = ea.attribute_id)
        LEFT JOIN tdshop_product_entity_varchar AS pe_varchar ON (pe_varchar.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_int AS pe_int ON (pe_int.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_text AS pe_text ON (pe_text.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_decimal AS pe_decimal ON (pe_decimal.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_datetime AS pe_datetime ON (pe_datetime.attribute_id = att.attribute_id)
) AS tbl ON ( tbl.entity_id = pe.entity_id)
GROUP BY pe.entity_id,tbl.attribute_id
ORDER BY eid DESC

提前致谢。

3 个答案:

答案 0 :(得分:1)

无法测试,但它会以某种方式

$users = DB::table('tdshop_product_category')
            ->join('tdshop_product_entity', 'pc.product_id' = 'pe.entity_id', 'pe.deleted_at' = NULL)
            ->leftJoin(DB::raw("SELECT 
            att.attribute_id,
            att.attribute_code,
          CASE att.backend_type
           WHEN `varchar` THEN pe_varchar.`value`
           WHEN 'int' THEN pe_int.`value`
           WHEN 'text' THEN pe_text.`value`
           WHEN 'decimal' THEN pe_decimal.`value`
           WHEN 'datetime' THEN pe_datetime.`value`
           ELSE att.backend_type
          END AS `value`,
          CASE att.backend_type
           WHEN 'varchar' THEN pe_varchar.`entity_id`
           WHEN 'int' THEN pe_int.`entity_id`
           WHEN 'text' THEN pe_text.`entity_id`
           WHEN 'decimal' THEN pe_decimal.`entity_id`
           WHEN 'datetime' THEN pe_datetime.`entity_id`
           ELSE att.backend_type
          END AS `entity_id`
        FROM tdshop_entity_attribute AS ea 
        LEFT JOIN tdshop_attribute AS att ON (att.attribute_id = ea.attribute_id)
        LEFT JOIN tdshop_product_entity_varchar AS pe_varchar ON (pe_varchar.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_int AS pe_int ON (pe_int.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_text AS pe_text ON (pe_text.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_decimal AS pe_decimal ON (pe_decimal.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_datetime AS pe_datetime ON (pe_datetime.attribute_id = att.attribute_id)
) tbl"), function($join){
    $join->on('tbl.entity_id', '=', 'pe.entity_id')
})->groupBy('pe.entity_id,tbl.attribute_id')->orderBy('eid', 'DESC')->get();

或者可能需要稍加修改。

答案 1 :(得分:1)

有时您可能需要在查询中使用原始表达式。这些表达式将作为字符串注入到查询中,因此请注意不要创建任何SQL注入点!要创建原始表达式,可以使用DB :: raw方法:

DB::table('combinations')
->select('combinations.id', DB::raw('GROUP_CONCAT(diseases.name ORDER BY diseases.id) AS DieasesName'))
->join('diseases', function($join) {
    $join->on(DB::raw('FIND_IN_SET(diseases.id, combinations.diseases_id)'), '>', 0);
})

或者如果你想使用DB::select小心SQL注入..请使用以下示例:

$results = DB::select('select * from users where id = :id', ['id' => 1]);

$results = DB::select('select * from users where id = ?', [1]);
你的代码中的

$sql = "
SELECT 
        pe.entity_id AS eid,
        tbl.attribute_id AS attr_id,
        tbl.attribute_code AS attr_code,
        tbl.`value`
FROM tdshop_product_category AS pc 
INNER JOIN tdshop_product_entity  AS pe ON ( pc.product_id = pe.entity_id AND pe.deleted_at IS NULL)
LEFT JOIN (
        SELECT 
            att.attribute_id,
            att.attribute_code,
          CASE att.backend_type
           WHEN 'varchar' THEN pe_varchar.`value`
           WHEN 'int' THEN pe_int.`value`
           WHEN 'text' THEN pe_text.`value`
           WHEN 'decimal' THEN pe_decimal.`value`
           WHEN 'datetime' THEN pe_datetime.`value`
           ELSE att.backend_type
          END AS `value`,
          CASE att.backend_type
           WHEN 'varchar' THEN pe_varchar.`entity_id`
           WHEN 'int' THEN pe_int.`entity_id`
           WHEN 'text' THEN pe_text.`entity_id`
           WHEN 'decimal' THEN pe_decimal.`entity_id`
           WHEN 'datetime' THEN pe_datetime.`entity_id`
           ELSE att.backend_type
          END AS `entity_id`
        FROM tdshop_entity_attribute AS ea 
        LEFT JOIN tdshop_attribute AS att ON (att.attribute_id = ea.attribute_id)
        LEFT JOIN tdshop_product_entity_varchar AS pe_varchar ON (pe_varchar.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_int AS pe_int ON (pe_int.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_text AS pe_text ON (pe_text.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_decimal AS pe_decimal ON (pe_decimal.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_datetime AS pe_datetime ON (pe_datetime.attribute_id = att.attribute_id)
) AS tbl ON ( tbl.entity_id = pe.entity_id)
GROUP BY pe.entity_id,tbl.attribute_id
ORDER BY eid DESC
";
$results = DB::select($sql);

答案 2 :(得分:0)

如果你有这样复杂的查询,我建议你使用DB::select方法。

DB::select("SELECT 
    pe.entity_id AS eid,
        tbl.attribute_id AS attr_id,
        tbl.attribute_code AS attr_code,
        tbl.`value`
    FROM tdshop_product_category AS pc 
    INNER JOIN tdshop_product_entity  AS pe ON ( pc.product_id = pe.entity_id AND pe.deleted_at IS NULL)
    LEFT JOIN (
        SELECT 
                att.attribute_id,
                att.attribute_code,
            CASE att.backend_type
                WHEN 'varchar' THEN pe_varchar.`value`
                WHEN 'int' THEN pe_int.`value`
                WHEN 'text' THEN pe_text.`value`
                WHEN 'decimal' THEN pe_decimal.`value`
                WHEN 'datetime' THEN pe_datetime.`value`
                ELSE att.backend_type
            END AS `value`,
            CASE att.backend_type
                WHEN 'varchar' THEN pe_varchar.`entity_id`
                WHEN 'int' THEN pe_int.`entity_id`
                WHEN 'text' THEN pe_text.`entity_id`
                WHEN 'decimal' THEN pe_decimal.`entity_id`
                WHEN 'datetime' THEN pe_datetime.`entity_id`
                ELSE att.backend_type
            END AS `entity_id`
        FROM tdshop_entity_attribute AS ea 
        LEFT JOIN tdshop_attribute AS att ON (att.attribute_id = ea.attribute_id)
        LEFT JOIN tdshop_product_entity_varchar AS pe_varchar ON (pe_varchar.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_int AS pe_int ON (pe_int.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_text AS pe_text ON (pe_text.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_decimal AS pe_decimal ON (pe_decimal.attribute_id = att.attribute_id)
        LEFT JOIN tdshop_product_entity_datetime AS pe_datetime ON (pe_datetime.attribute_id = att.attribute_id)
    ) AS tbl ON ( tbl.entity_id = pe.entity_id)
    GROUP BY pe.entity_id,tbl.attribute_id
    ORDER BY eid DESC");