ORDER BY Not working with GROUP BY - php

I have written sql code using group by and order by but in here order by not working can any one help me
SELECT " . DB_PREFIX . "leaderboard_scores.* , SUM(" . DB_PREFIX . "leaderboard_scores.score) as total_score, " . DB_PREFIX . "customer.firstname,
" . DB_PREFIX . "customer.lastname
FROM " . DB_PREFIX . "leaderboard_scores
JOIN " . DB_PREFIX . "customer ON " . DB_PREFIX . "leaderboard_scores.philips_store_id = " . DB_PREFIX . "customer.customer_id
GROUP BY " . DB_PREFIX . "leaderboard_scores.phi_store_id
ORDER BY " . DB_PREFIX . "leaderboard_scores.week DESC
This query work without php errors but given middle rows without giving top weeks. weeks store using times stamp
actual query
SELECT rc_leaderboard_scores.* , SUM(rc_leaderboard_scores.score)
as total_score, rc_customer.firstname, rc_customer.lastname
FROM rc_leaderboard_scores
JOIN rc_customer
ON rc_leaderboard_scores.phi_store_id = rc_customer.customer_id
GROUP BY rc_leaderboard_scores.phi_store_id
ORDER BY rc_leaderboard_scores.week DESC

If you need to return the last record for every customer, you need a JOIN with a subquery that returns MAX(week) for every customer id:
SELECT
rc_leaderboard_scores.*,
m.total_score,
rc_customer.firstname,
rc_customer.lastname
FROM
(SELECT phi_store_id,
MAX(`week`) as max_week,
SUM(score) as total_score,
FROM rc_leaderboard_scores
GROUP BY phi_store_id) m
INNER JOIN
rc_leaderboard_scores
ON rc_leaderboard_scores.phi_store_id = m.phi_store_id
AND rc_leaderboard_scores.`week` = m.max_week
INNER JOIN
rc_customer ON m.phi_store_id = rc_customer.customer_id

Related

How to get another column values from MySQL Query in opencart3 admin panel?

The code below uses 2 tables "category_path" and "category_description" to get id=>name of all categories and sub-categories. Im bad at mySql, so I would appreciate if you help me.
In this function I need to also get values of 'cat_name' column FROM THE OTHER (third) table named 'category'
https://i.stack.imgur.com/1PC4A.jpg
public function getCategories($data = array()) {
$sql = "SELECT cp.category_id AS category_id,
GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR ' > ') AS name,
c1.parent_id, c1.sort_order
FROM " . DB_PREFIX . "category_path cp
LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id)
LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id)
WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'";
You are already using that category table in the first join and you gave it an alias of c1 so
SELECT cp.category_id AS category_id, c1.cat_name,
. . .
. . .

combine left join into my queries but failed

Tried to combine another query to my existing queries but failed.
Here is the working query without LEFT JOIN
$order_query = $this->db->query("SELECT * FROM " . DB_PREFIX .
"order_items WHERE order_id = '" . (int)$order_id . "'");
Below is the one tried to combined (not working)
$order_query = $this->db->query("SELECT * FROM " . DB_PREFIX .
"order_items WHERE order_id = '" . (int)$order_id . "'" LEFT JOIN .
DB_PREFIX ."item_description WHERE item_id= '" . (int)$item_id . "'"
);
Any idea where did i do wrong ?
You need to construct the join statement correctly. The WHERE clauses come in later.
$prefix = DB_PREFIX;
$sql = "
SELECT oi.* FROM {$prefix}order_items AS oi
LEFT JOIN {$prefix}item_description AS itd
ON oi.item_id = itd.item_id
WHERE oi.order_id = ?
AND itd.item_id = ?
";
$this->db->query($sql, [$order_id, $item_id]);
You can try this code
$order_query = $this->db->query("SELECT oi.* FROM " . DB_PREFIX ."order_items oi
LEFT JOIN ".DB_PREFIX ."item_description des on oi.order_id = des.order_id
WHERE oi.order_id = '" . (int)$order_id . "' and
des.item_id= '" . (int)$item_id . "'");

mysql select something as where is not null

I'm quite new at this, so I have question about MySQL. I have query in openCart:
$sql = "SELECT cp.category_id AS category_id,
GROUP_CONCAT(cd1.name
ORDER BY cp.level SEPARATOR ' > ') AS name,
c1.parent_id,
c1.sort_order
FROM " . DB_PREFIX . "category_path cp
LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id)
LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id)
WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND cd2.language_id = '" . (int)$this->config->get('config_language_id') ."'";
In MySQL I added new column "show_menu" (it can be NULL or 1) and now I want to change my query, that It will return only categories in where show_menu = 1
As I understand I need something like: WHERE show_menu IS 1.
Maybe anyone can help where to put it? because I tried, but no luck...
Use show_menu = 1 or show_menu IS NOT NULL
You just can't use the '=' operator with a null value.
Just add the following to your WHERE clause:
... AND show_menu = 1
Just Like
SELECT * FROM tablename
WHERE conditions_from_your_existing_query
AND show_menu = 1
You can use show_menu = 1 or you can use show_menu is not null. Either way will work.

Select query returning 1 result instead of 3 because of AVG

The select query below returns 1 row when it should be 3. I am pretty sure it is because of the AVG(k.sumtotal) field.
If I rewrite the query and take out that AVG(k.sumtotal) column and take out the FROM inv_ratings AS k, I get my 3 rows.
I can't figure out why this table (inv_ratings) or this AVG(k.sumtotal) column restrict the number of rows to 1. I looked online for hours trying to find information about returning results using the AVG clause and didn't find much. Do I have to use a group by clause, I tried that and only get errors.
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";
$q = mysqli_query($dbc, $p) or trigger_error("Query: $p\n<br />mysqli Error: " . mysqli_error($dbc));
You are running into one of MySQL's gotchas:
http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html
I hate that MySQL ever allows this syntax because it only causes confusion. But what you probably want is (to use MySQL's hackish behavior, please note that if there are multiple values for any of the fields besides invention_id or sumtotal, you get a random value from that column):
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " GROUP BY i.invention_id "
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";
Or, to not use MySQL's hackish behavior:
$p = "SELECT i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name, AVG(k.sumtotal)"
. " FROM inv_ratings AS k INNER JOIN inventions AS i USING (invention_id)"
. " INNER JOIN categories AS c USING (category_id)"
. " INNER JOIN images AS u USING (invention_id)"
. " WHERE c.category_id = $cat AND i.approved = 'approved'"
. " GROUP BY i.invention_id, i.inv_title, i.date_submitted, i.category_id,"
. " i.approved, c.category_id, c.category, u.image_name "
. " HAVING u.image_name < 2"
. " ORDER BY date_submitted"
. " DESC LIMIT $start, $display";

Opencart: Product Name Print Several times, How to fix.?

i had added the following Opencart module for my order report list...
http://www.opencart.com/index.php?route=extension/extension/info&extension_id=3597&filter_search=order%20list%20filter%20model&page=4
I have problems with the column "Products". If there are more than one option the products name prints several times. So if I got a product with three options the product name prints three times. Is there any way to fix this problem?
i want print product name and model number only once, any idea.?
i will attach the results what i got now...
this is my sql query...
public function getOrders($data = array()) {
$sql = "select o.order_id,o.email,o.telephone,CONCAT(o.shipping_address_1, ' ', o.shipping_address_2) AS address,CONCAT(o.firstname, ' ', o.lastname) AS customer,o.payment_zone AS state,o.payment_address_2 AS block, o.payment_address_1 AS address,o.payment_postcode AS postcode,(SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . "') AS status,o.payment_city AS city,GROUP_CONCAT(pd.name) AS pdtname,GROUP_CONCAT(op.model) AS model,o.date_added,sum(op.quantity) AS quantity,GROUP_CONCAT(opt.value ) AS options, GROUP_CONCAT(opt.order_product_id ) AS ordprdid,GROUP_CONCAT(op.order_product_id ) AS optprdid, GROUP_CONCAT(op.quantity) AS opquantity from `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_product op ON (op.order_id = o.order_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id = op.product_id and pd.language_id = '" . (int)$this->config->get('config_language_id') . "') LEFT JOIN " . DB_PREFIX . "order_option opt ON (opt.order_product_id = op.order_product_id) ";
Product Name = GROUP_CONCAT(pd.name) AS pdtname,
Add DISTINCT
GROUP_CONCAT(DISTINCT pd.name)

Categories