Advertisement
techouse

sql

Apr 19th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. public function getUsedCategories() {
  2.         global $wpdb;
  3.         $vouchers_table_name = $wpdb->prefix . 'voucherpress_vouchers';
  4.         $categories_table_name = $wpdb->prefix . 'voucherpress_categories';
  5.         $category_parents_table_name = $wpdb->prefix . 'voucherpress_category_parents';
  6.         return $wpdb->get_results($wpdb->prepare(
  7.             "
  8.            SELECT
  9.                cp.name AS parent,
  10.                GROUP_CONCAT(
  11.                    DISTINCT
  12.                    c.id
  13.                    ORDER BY c.id
  14.                    ASC
  15.                    SEPARATOR ';'
  16.                ) AS ids,
  17.                GROUP_CONCAT(
  18.                    DISTINCT
  19.                    c.name
  20.                    ORDER BY c.id
  21.                    ASC
  22.                    SEPARATOR ';'
  23.                ) AS categories              
  24.            FROM $categories_table_name AS c
  25.                INNER JOIN $vouchers_table_name AS v
  26.                    ON c.id = v.category_id
  27.                INNER JOIN $category_parents_table_name AS cp
  28.                    ON c.parent = cp.id
  29.            ORDER BY cp.id
  30.            ASC
  31.            "
  32.         ));
  33.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement