How Can I use combination of inner join and order by - mysql

How Can I use combination of inner join and order by.
My query is running but my output is not giving me the correct answer, im having problem with this line, how can I sort 1 column or the column cm_status only:
INNER JOIN dbfeedersystem.tblrepair on
tblpm.control_number_pm=tblrepair.control_number
SELECT tblfeeder.control_number AS Control_No,tblfeeder.serial_number AS
Serial_No,tblfeeder.model AS Model,
DATE_FORMAT(date_updated_pm,'%d-%b-%Y') AS PM_Date,
DATE_FORMAT(DATE_SUB(due_date,INTERVAL 1 WEEK),'%d-%b-%Y') AS Issuance_of_Recall,
DATE_FORMAT(due_date,'%d-%b-%Y') AS Due_Date, pm_status, cm_status
FROM dbfeedersystem.tblpm INNER JOIN dbfeedersystem.tblrepair on
tblpm.control_number_pm=tblrepair.control_number
INNER JOIN
dbfeedersystem.tblfeeder on
tblpm.control_number_pm=tblfeeder.control_number
INNER JOIN
(SELECT control_number_pm,max(date_updated_pm) AS date_updated_pm FROM
dbfeedersystem.tblpm GROUP BY control_number_pm)
AS MAX USING (control_number_pm,date_updated_pm)
ORDER BY date_updated_pm,control_number_pm;

Related

How to make Group By With multi inner join

i try to make filttering for selected data using inner join
i have 3 tables i should join it
this is my query
SELECT DISTINCT *
FROM products_products
INNER JOIN products_products_translations on products_products.id = products_products_translations.entry_id
INNER JOIN products_products_related on products_products.id = products_products_related.entry_id
INNER JOIN upload_files on products_products_related.related_id = upload_files.related_id
WHERE products_products_translations.locale = 'tr'
GROUP BY products_products_related.entry_id;
And this is the error that i got
Query 1 ERROR: Expression #35 of SELECT list is not in GROUP BY clause and contains nonaggregated column
'shop.products_products_translations.id' which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by
Remove the distinct, and add products_products_related.entry_id, COUNT(*)
SELECT products_products_related.entry_id, COUNT(*)
FROM products_products
INNER JOIN products_products_translations on products_products.id = products_products_translations.entry_id
INNER JOIN products_products_related on products_products.id = products_products_related.entry_id
INNER JOIN upload_files on products_products_related.related_id = upload_files.related_id
WHERE products_products_translations.locale = 'tr'
GROUP BY products_products_related.entry_id;
You can't directly select the columns that are not coming after the group by clause.
The reason is when you group by any column, entry_id for example, you'll have single row for each entry_id. But, each entry_id has many rows in the original table with many different values in the other columns.
Therefore, you need to tell the query how to aggregate the columns which are not the columns you decided to group by.
You can aggregate them by COUNT(...) or SUM(...), or many other functions.
The solution would be something like the following:
SELECT products_products_related.entry_id, MAX(Column1), MIN(Column2) /*etc...*/
FROM products_products
INNER JOIN products_products_translations on products_products.id = products_products_translations.entry_id
INNER JOIN products_products_related on products_products.id = products_products_related.entry_id
INNER JOIN upload_files on products_products_related.related_id = upload_files.related_id
WHERE products_products_translations.locale = 'tr'
GROUP BY products_products_related.entry_id;

How can I grab all rows from one table, but filter the second table?

I am running a query on a PHP page that will pull all records from one table, INNER JOIN with two other tables and then list all of the results. However on the second table I only want the most recent record.
Here is my query
SELECT * FROM wn_trailer
INNER JOIN (
SELECT id, trailer_id, trailer_status, trailer_assigned, MAX(last_update), trailer_lat, trailer_long
FROM wn_trailer_history
) AS th ON wn_trailer.id = th.trailer_id
INNER JOIN wn_trailer_status ON wn_trailer_status.id = th.trailer_status
INNER JOIN wn_users ON wn_users.id = th.trailer_assigned
ORDER BY trailer_number ASC
The query runs but returns only the first record.
You want an additional JOIN to bring in the data on the last update date. Also, your subquery needs a GROUP BY:
SELECT *
FROM wn_trailer t INNER JOIN
(SELECT trailer_id, MAX(last_update) as max_last_update
FROM wn_trailer_history
GROUP BY trailer_id
) tht
ON t.id = tht.trailer_id INNER JOIN
wn_trailer_history th
ON th.trailer_id = tht.trailer_id AND
th.last_update = tht.max_last_update INNER JOIN
wn_trailer_status ts
ON ts.id = th.trailer_status INNER JOIN
wn_users u
ON u.id = th.trailer_assigned
ORDER BY trailer_number ASC;
I also added table aliases so the query is easier to write and to read.

PHP MySQL: Join 3 tables but

I have 3 tables, errorcode_table, description_table, and customer_table.
The query below will display all records that are in the errorcode_table and I have an inner join that will also display the customer_table as per the serial number in both tables.
SELECT
errorcode_table.error,
errorcode_table.deviceserialnumber,
customer_table.serialnumber,
customer_table.customer,
FROM errorcode_table
INNER JOIN customer_table
ON errorcode_alert_table.deviceserialnumber = customerinfo_table.serialnumber
Now I want to also display the description of the error code as well, here's my attempt:
SELECT
errorcode_table.error,
errorcode_table.serialnumber,
customer_table.serialnumber,
customer_table.customer,
description.serialnumber
description.info
FROM errorcode_table
INNER JOIN customer_table
RIGHT JOIN description_table
ON errorcode_table.deviceserialnumber = customer_table.serialnumber
ON errorcode_table.deviceserialnumber = description_table.serialnumber
Now I'm not getting any records. Please assist.
The ON clause for each join should appear immediately after each join condition. And you can introduce table aliases to make the query easier to read.
SELECT
e.error,
e.serialnumber,
c.serialnumber,
c.customer,
d.serialnumber,
d.info
FROM errorcode_table e
INNER JOIN customer_table c
ON e.deviceserialnumber = c.serialnumber
RIGHT JOIN description_table d
ON e.deviceserialnumber = d.serialnumber;

How to access parent column from a subquery within a join

I'm trying to left join the second table useri_ban based on the users' ids, with the extra condition: useri_ban.start_ban = max_start.
In order for me to calculate max_start, I have to run the following subquery:
(SELECT MAX(ub.start_ban) AS max_start, user_id FROM useri_ban ub WHERE ub.user_id = useri.id)
Furthermore, in order to add max_start to every row, I need to inner join this subquery's result into the main result. However, it seems that once I apply that join, the subquery is no longer able to access useri.id.
What am I doing wrong?
SELECT
useri.id as id,
useri.email as email,
useri_ban.warning_type_id as warning_type_id,
useri_ban.type as type,
useri.created_at AS created_at
FROM `useri`
inner join
(SELECT MAX(ub.start_ban) AS max_start, user_id FROM useri_ban ub WHERE ub.user_id = useri.id) `temp`
on `useri`.`id` = `temp`.`user_id`
left join `useri_ban` on `useri_ban`.`user_id` = `useri`.`id` and `useri_ban`.`start_ban` = `max_start`
Does this solve your problem? You need GROUP BY in the inner query instead of another join.
SELECT useri.id, useri.email, maxQuery.maxStartBan
FROM useri
INNER JOIN
(
SELECT useri_ban.user_id ubid, MAX(useri_ban.startban) maxStartBan
FROM useri_ban
GROUP BY useri_ban.user_id
) AS maxQuery
ON maxQuery.ubid = useri.id;

fetching records with long sql query with multple joins

I will try to explain things as much as I can.
I have following query to fetch records from different tables.
SELECT
p.p_name,
p.id,
cat.cat_name,
p.property_type,
p.p_type,
p.address,
c.client_name,
p.price,
GROUP_CONCAT(pr.price) AS c_price,
pd.land_area,
pd.land_area_rp,
p.tagline,
p.map_location,
r.id,
p.status,
co.country_name,
p.`show`,
u.name,
p.created_date,
p.updated_dt,
o.type_id,
p.furnished,
p.expiry_date
FROM
property p
LEFT OUTER JOIN region AS r
ON p.district_id = r.id
LEFT OUTER JOIN country AS co
ON p.country_id = co.country_id
LEFT OUTER JOIN property_category AS cat
ON p.cat_id = cat.id
LEFT OUTER JOIN property_area_details AS pd
ON p.id = pd.property_id
LEFT OUTER JOIN sc_clients AS c
ON p.client_id = c.client_id
LEFT OUTER JOIN admin AS u
ON p.adminid = u.id
LEFT OUTER JOIN sc_property_orientation_type AS o
ON p.orientation_type = o.type_id
LEFT OUTER JOIN property_amenities_details AS pad
ON p.id = pad.property_id
LEFT OUTER JOIN sc_commercial_property_price AS pr
ON p.id = pr.property_id
WHERE p.id > 0
AND (
p.created_date > DATE_SUB(NOW(), INTERVAL 1 YEAR)
OR p.updated_dt > DATE_SUB(NOW(), INTERVAL 1 YEAR)
)
AND p.p_type = 'sale'
everything works fine if I exclude GROUP_CONCAT(pr.price) AS c_price, from above query. But when I include this it just gives one result. My intention to use group concat above is to fetch comma separated price from table sc_commercial_property_price that matches the property id in this case p.id. If the records for property exist in sc_commercial_property_price then fetch them in comma separated form along with other records. If not it should return blank. What m I doing wrong here?
I will try to explain again if my problem is not clear. Thanks in advance
The GROUP_CONCAT is an aggregation function. When you include it, you are telling SQL that there is an aggregation. Without a GROUP BY, only one row is returns, as in:
select count(*)
from table
The query that you have is acceptable syntax in MySQL but not in any other database. The query does not automatically group by the columns with no functions. Instead, it returns an arbitrary value. You could imagine a function ANY, so you query is:
select any(p.p_name) as p_num, any(p.tagline) as tagline, . . .
To fix this, put all your current variables in a group by clause:
GROUP BY
p.p_name,
p.id,
cat.cat_name,
p.property_type,
p.p_type,
p.address,
c.client_name,
p.price,
pd.land_area,
pd.land_area_rp,
p.tagline,
p.map_location,
r.id,
p.status,
co.country_name,
p.`show`,
u.name,
p.created_date,
p.updated_dt,
o.type_id,
p.furnished,
p.expiry_date
Most people who write SQL think it is good form to include all the group by variables in the group by clause, even though MySQL does not necessarily require this.
Add GROUP BY clause enumerating whatever you intend to have separate rows for. What happens now is that it picks some value for each result column and group_concats every pr.price.