I need to merge this query
SELECT
*,
(SELECT
CONCAT(c.firstname, ' ', c.lastname)
FROM
wwwpser_customer c
WHERE
c.customer_id = o.customer_id) AS customer
FROM
wwwpser_order o
WHERE
o.order_id = '20'
with
SELECT
orders . *, wwwpser_comuna.provincia_id AS payment_provincia_id
FROM
wwwpser_order orders
LEFT JOIN
wwwpser_comuna ON (orders.payment_city = wwwpser_comuna.comuna_id)
SQL syntax is new for me so I need a little help with this, thx
SELECT CONCAT(c.firstname, ' ', c.lastname)
, wc.provincia_id
, o.*
FROM wwwpser_order o
LEFT JOIN
wwwpser_customer c
ON c.customer_id = o.customer_id
LEFT JOIN
wwwpser_comuna wc
ON wc.comuna_id = o.payment_city
WHERE o.order_id = 20
It looks like this should be your solution.
SELECT
o.*, -- modified (qualified the
(SELECT -- asterisk)
CONCAT(c.firstname, ' ', c.lastname)
FROM
wwwpser_customer c
WHERE
c.customer_id = o.customer_id) AS customer,
c.provincia_id AS payment_provincia_id -- added from second query
FROM
wwwpser_order o
LEFT JOIN -- added from second query
wwwpser_comuna c ON (o.payment_city = c.comuna_id) -- added from second query
WHERE
o.order_id = '20'
I strongly recommend learning about the general SQL syntax, though. There are some useful examples in the MySQL tutorials
Related
Good afternoon,
I'm trying to get some information from my MySQL database and I'm having problems because I'm not able to have the information needed. I have tried a lot of different approaches and none of them have worked. I hope you can find something because I'm very close to find the solution but something is missing:
MySQL query:
SELECT b.id, b.tipo_perfil, round(avg(b.edad)), COUNT(c.zona), c.zona
FROM analizador_datos_usuario AS a
INNER JOIN analizador_datos_perfil AS b ON (a.id_usuario = b.id_perfil)
INNER JOIN analizador_datos_perfil_historial AS c ON (b.id = c.id_perfil)
WHERE a.id_usuario=21
GROUP BY b.tipo_perfil, c.zona
ORDER BY b.tipo_perfil ASC, count(c.zona) DESC
This query gives me the following information:
Table (in red it's what I need):
Kind regards,
try that :
SELECT b.tipo_perfil, round(avg(b.edad)), COUNT(distinct c.zona), group_concat(distinct b.id separator ' ') as id_list, group_concat(distinct c.zona separator ' ') as zona_list
FROM analizador_datos_usuario AS a
INNER JOIN analizador_datos_perfil AS b ON (a.id_usuario = b.id_perfil)
INNER JOIN analizador_datos_perfil_historial AS c ON (b.id = c.id_perfil)
WHERE a.id_usuario=21
GROUP BY b.tipo_perfil
ORDER BY b.tipo_perfil ASC, count(distinct c.zona) DESC
I think you are getting result what is displayed and you want result which is in red colour.
Try this modified query:-
SELECT b.id, b.tipo_perfil, round(avg(b.edad)), COUNT(c.zona) counted_zone, c.zona
FROM analizador_datos_usuario AS a
INNER JOIN analizador_datos_perfil AS b ON (a.id_usuario = b.id_perfil)
INNER JOIN analizador_datos_perfil_historial AS c ON (b.id = c.id_perfil)
WHERE a.id_usuario=21
GROUP BY b.tipo_perfil, c.zona
Having MAX(counted_zone)
ORDER BY b.tipo_perfil ASC, counted_zone DESC
I want to order 2 tables by date, but the problem is sql is not ordering them simultaneously.
here is my query:
SELECT users_id,CONCAT_WS(' ', users_fname, users_lname)
AS full_name, reply_message, concern_message
FROM tbl_usersinfo AS i
LEFT JOIN tbl_concern AS c
ON c.student_id = i.users_id
LEFT JOIN tbl_reply_concern AS r
ON r.student_id = i.users_id
ORDER BY c.date,r.date
I read that i need to put ISNULL(c.date,r.date) but its not working.
Try ifnull or coalesce
SELECT users_id,
CONCAT_WS(' ', users_fname, users_lname) AS full_name,
reply_message,
concern_message
FROM tbl_usersinfo AS i
LEFT JOIN tbl_concern AS c
ON c.student_id = i.users_id
LEFT JOIN tbl_reply_concern AS r
ON r.student_id = i.users_id
ORDER BY ifnull(c.date, r.date)
The MySQL equivalent of ISNULL (in SQL Server) is IFNULL. In MySQL I believe ISNULL is just a test of whether something is or is not null which would evaluate to 0 or 1.
Below is my query which I need to optimize.
SELECT
UPPER(IFNULL(op.supplier_payment_method,s.default_payment_method)) AS Payment_Method,
op.supplier_payment_date AS Payment_Date,
Date_format(IFNULL(op.supplier_payment_date,op.ship_date),'%M %Y') AS Payment_Month,
s.supplier_name AS Farm,
op.sub_order_id AS Order_num,
Date_format(op.ship_date,'%b-%d-%Y') AS Ship_Date,
op.farm_credit AS Farm_Credit,
op.credit_memo AS Credit_Memo,
op.credit_description AS Credit_Description,
opb.boxes AS Box_Type,
CONCAT('$',FORMAT(op.box_charge,2)) AS Box_Charge,
op.invoice_num AS Invoice_num,
CONCAT('$',FORMAT(op.invoice_amt,2)) AS Invoice_Amt,
CONCAT('$',FORMAT(op.total_invoice_amt,2)) AS Total_Invoice_Amt,
CONCAT(op.UM_qty,' ',op.UM_type) AS St_Bu_Qty,
op.PO_Product_Name AS Invoice_desc,
CONCAT('$',FORMAT((op.price_um*op.um_qty),2)) AS Cost_product_cms,
op.supplier_invoice_note AS Supplier_Invoice_Notes,
CONCAT('$',FORMAT(op.cms_invoice_cost,2)) AS CMS_Invoice_diff,
CONCAT('$',FORMAT(op.total_farm_cost,2)) AS Farm_Cost
FROM
orders_products op
INNER JOIN
suppliers s ON s.supplier_id = op.supplier_name
LEFT JOIN
(
SELECT
sub_order_id,
GROUP_CONCAT(CONCAT(box_type_qty,' ',bo.box_option_name) SEPARATOR ', ') AS boxes
FROM
order_products_boxes opb
INNER JOIN box_options bo ON bo.id=opb.box_type_id
GROUP BY
opb.sub_order_id
) opb ON opb.sub_order_id = op.sub_order_id
WHERE
op.order_active=0
AND op.ship_date>='2013-03-01'
AND op.ship_date<='2013-04_01'
ORDER BY op.ship_date DESC
As you can see, the query comprises of 4 tables, each containing around 20k-30k rows each. So as soon as I add in the sub-query, the query becomes exceptionally slow. It takes approx 1.5mins to fetch just 500 rows of record. Is there a way to speed things up within a single query?
Your left-join query that pre-concatenates the sub-box options is querying the ENTIRE DATABASE FIRST, then only joins to those that are within the criteria you are limiting for the outer WHERE clause. It may be a little more effort, but pull the same outer criteria into your inner query should significantly help. As for doing a field-level sql-select on a per-every-row basis could be a performance killer.
You should only need to change at the "FROM" clause...
from
orders_products op
INNER JOIN suppliers s
ON op.supplier_name = s.supplier_id
LEFT JOIN
( SELECT
opb2.sub_order_id,
GROUP_CONCAT(CONCAT(box_type_qty,' ',bo.box_option_name) SEPARATOR ', ') AS boxes
FROM
orders_products op2
JOIN order_products_boxes opb2
on op2.sub_order_id = opb2.sub_order_id
INNER JOIN box_options bo
ON opb2.box_type_id = bo.id
WHERE
op2.order_active = 0
AND op2.ship_date >= '2013-03-01'
AND op2.ship_date <= '2013-04_01'
GROUP BY
opb2.sub_order_id ) opb
ON op.sub_order_id = opb.sub_order_id
WHERE
op.order_active = 0
AND op.ship_date >= '2013-03-01'
AND op.ship_date <= '2013-04_01'
SELECT
UPPER(IFNULL(op.supplier_payment_method,s.default_payment_method)) AS Payment_Method,
op.supplier_payment_date AS Payment_Date,
Date_format(IFNULL(op.supplier_payment_date,op.ship_date),'%M %Y') AS Payment_Month,
s.supplier_name AS Farm,
op.sub_order_id AS Order_num,
Date_format(op.ship_date,'%b-%d-%Y') AS Ship_Date,
op.farm_credit AS Farm_Credit,
op.credit_memo AS Credit_Memo,
op.credit_description AS Credit_Description,
(
SELECT
GROUP_CONCAT(CONCAT(box_type_qty,' ',bo.box_option_name) SEPARATOR ', ')
FROM
order_products_boxes opb
INNER JOIN box_options bo ON bo.id=opb.box_type_id
GROUP BY
opb.sub_order_id
) AS Box_Type,
CONCAT('$',FORMAT(op.box_charge,2)) AS Box_Charge,
op.invoice_num AS Invoice_num,
CONCAT('$',FORMAT(op.invoice_amt,2)) AS Invoice_Amt,
CONCAT('$',FORMAT(op.total_invoice_amt,2)) AS Total_Invoice_Amt,
CONCAT(op.UM_qty,' ',op.UM_type) AS St_Bu_Qty,
op.PO_Product_Name AS Invoice_desc,
CONCAT('$',FORMAT((op.price_um*op.um_qty),2)) AS Cost_product_cms,
op.supplier_invoice_note AS Supplier_Invoice_Notes,
CONCAT('$',FORMAT(op.cms_invoice_cost,2)) AS CMS_Invoice_diff,
CONCAT('$',FORMAT(op.total_farm_cost,2)) AS Farm_Cost
FROM
orders_products op
INNER JOIN
suppliers s ON s.supplier_id = op.supplier_name
LEFT JOIN
order_products_boxes opb (
INNER JOIN box_options bo ON bo.id=opb.box_type_id
) opb ON opb.sub_order_id = op.sub_order_id
WHERE
op.order_active=0
AND op.ship_date>='2013-03-01'
AND op.ship_date<='2013-04_01'
ORDER BY op.ship_date DESC
I am trying to write a query that shows me all orders with their options. So far that's done, but when I try to group them by order id it just print a separate row for each option and not the order ID and all its option under it.
I am missing something but can't figure out what is it :)
Here is what I have
$sql = 'SELECT *
FROM `order` RIGHT JOIN `order_option`
on order.order_id=order_option.order_id
where order_status_id = 2
group by order_option.order_option_id';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
print "ID:{$row['order_id']} <br> ".
"OPTION NAME: {$row['name']} <br> ".
"VALUE: {$row['value']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
**UPDATE 1**
This is how the query worked now
SELECT
o.order_id,
o.name,
o.value,
GROUP_CONCAT(o.order_id, o.name, o.value SEPARATOR ',') AS Options
FROM `order_option` AS o
LEFT JOIN `order` AS oo on o.order_id = oo.order_id
where oo.order_status_id = 2
group by o.order_id
You should GROUP BY order_id instead, and use GROUP_CONCAT to get all the options of an order in the same row:
SELECT
o.order_id,
GROUP_CONCAT(oo.order_option_id SEPARATOR ',') AS Options
FROM `order` AS o
RIGHT JOIN `order_option` AS oo on o.order_id = oo.order_id
where o.order_status_id = 2
group by o.order_id;
SQL Fiddle Demo
If GROUP_CONCAT is not what are you looking for, then you have to use other aggregate functions, but don't include any columns that is not in the GROUP BY clause in the SELECT clause without an aggregate function. Otherwise you will got inconsistent data.
Update 1
The reason your query doesn't work:
SELECT
o.order_id,
o.name,
o.value,
GROUP_CONCAT(o.order_id SEPARATOR ',') AS Options
FROM order_option AS o
LEFT JOIN order AS oo on o.order_id = oo.order_id
where oo.order_status_id = 2
group by o.order_id
Becuase the two columns o.name, o.value in the SELECT clause are not neither in an aggregate function not in the GROUP BY clause, so you are getting inconsistent values of these tow columns, MySQL gets an arbitrary value for them. Try this instead:
SELECT
o.order_id,
o.name,
o.value,
GROUP_CONCAT(o.order_id SEPARATOR ',') AS Options
FROM order_option AS o
LEFT JOIN order AS oo on o.order_id = oo.order_id
where oo.order_status_id = 2
group by o.order_id,
o.name,
o.value;
I'm doing a LEFT JOIN on three tables, where the table "time" doesn't necessarily contain any matching rows. But if no matching rows is found in that table, the linked data disappears.
SELECT
w.date AS worker_date,
w.name AS worker_name,
w.address AS worker_address,
w.zip AS worker_zip,
w.place AS worker_place,
w.phone AS worker_phone,
w.email AS worker_email,
w.company AS worker_company,
w.accessibility AS worker_accessibility,
c.date AS client_date,
c.name AS client_name,
c.address AS client_address,
c.zip AS client_zip,
c.place AS client_place,
c.phone AS client_phone,
c.email AS client_email,
c.web AS client_web,
c.contact AS client_contact,
j.date AS job_date,
j.client_id,
j.worker_id,
j.name AS job_name,
j.description AS job_description,
j.type AS job_type,
j.status AS job_status,
j.proof AS job_proof,
j.deadline AS job_deadline,
j.price AS job_price,
j.final_client AS job_final_client,
SUM(t.hours) AS time_hours
FROM
jobs AS j
LEFT JOIN (
workers AS w,
clients AS c,
time AS t
) ON (
w.id = j.worker_id AND
c.id = j.client_id AND
j.id = t.job_id
) GROUP BY
j.id;
How can I make this work?
Thank you in advance.
add
WHERE t.job_id IS NOT NULL before GROUP BY
Try Replace
SUM(t.hours) AS time_hours
to
(SELECT IFNULL(SUM(t.hours),0) FROM time WHERE time.job_id=j.job_id) AS time_hours
And remove the time from the join
I think your basic query is correct (with the join under braces)
Just replace
SUM(t.hours) AS time_hours
with
SUM(if(t.hours is NULL,0,t.hours)) AS time_hours
I am not sure if this is the problem here, but the behavior of commas vs JOINs changed after a certain MySQL version. Try this
...
FROM jobs AS j LEFT JOIN workers AS w ON w.id = j.worker_id
LEFT JOIN clients AS c c.id = j.client_id
LEFT JOIN `time` AS t ON j.id = t.job_id
...
Also modify the SUM with IFNULL as #ajreal suggests.