i am trying to list out the latest update from two table doc_to_do and doc_bug_tracker below is my table structure
doc_to_do
doc_bug_tracker
and this is my current query :
$sth = $this->db->prepare('SELECT p.*,
dtd.projects_id as dtd_projects_id, dtd.content as dtd_content, dtd.date_modified as dtd_date_modified,
dbt.projects_id as dbt_projects_id, dbt.content as dbt_content, dbt.date_modified as dbt_date_modified
FROM `projects` p LEFT JOIN `doc_to_do` dtd ON p.id=dtd.projects_id
LEFT JOIN `doc_bug_tracker` dbt ON p.id=dbt.projects_id
where p.id="'.$project_id.'"');
so now how to order by date_modified either from table doc_to_do or doc_bug_tracker ?
To get only the latest date (as opposed to all dates, ordered descending) try this:
SELECT
p.id,
MAX(GREATEST(dtd.date_modified, dbt.date_modified)) AS MaxDate
FROM projects p
LEFT JOIN doc_to_do dtd ON p.id = dtd.projects_id
LEFT JOIN doc_bug_tracker dbt ON p.id = dbt.projects_id
WHERE p.id = <project_id>
GROUP BY p.id
If you need additional columns in your SELECT, be sure to include them in your GROUP BY.
This should work :
'SELECT x.* FROM
(
SELECT p.*,
dtd.projects_id as dtd_projects_id, dtd.content as dtd_content, dtd.date_modified as dtd_date_modified,
dbt.projects_id as dbt_projects_id, dbt.content as dbt_content, dbt.date_modified as dbt_date_modified
FROM `projects` p LEFT JOIN `doc_to_do` dtd ON p.id=dtd.projects_id
LEFT JOIN `doc_bug_tracker` dbt ON p.id=dbt.projects_id
where p.id="'.$project_id.'"
) x
ORDER BY x.date_modified ASC'
Related
How to show multiple table list in mysql
I have a query that has two tables
one for patients and the second one for patients who has tested for covid-19
I want to show all the patients either they tested or not
If tested then show the result with his/her name
It only showing the patients who has tested for the covid not every one
how to solve that please ?
here's my Query
SELECT patient.*
,covidtest.covidTestResult ,subareas.areaname
FROM
( patient LEFT OUTER JOIN subareas
ON patient.town_id = subareas.town_id ) LEFT OUTER join covidtest ON
patient.Idnumber = covidtest.Idnumber where
patient.Idnumber=covidtest.Idnumber and
covidtest.CovidTestDate=(select max(covidtest.CovidTestDate)from
covidtest where patient.Idnumber=covidtest.Idnumber) group by covidtest.Idnumber
UNION SELECT patient.* ,covidtest.covidTestResult,subareas.areaname FROM
( patient LEFT OUTER JOIN subareas ON
patient.town_id = subareas.town_id ) LEFT OUTER join covidtest ON
patient.Idnumber = covidtest.Idnumber where patient.Idnumber=covidtest.Idnumber
and covidtest.CovidTestDate=(select max(covidtest.CovidTestDate) from covidtest where
patient.Idnumber=covidtest.Idnumber) group by covidtest.Idnumber;
here is what you need to do:
SELECT
patient.*,
covidtest.covidTestResult,
subareas.areaname
FROM patient
left join subareas on patient.town_id = subareas.town_id
Left join lateral
( select covidTestResult from covidtest
where patient.Idnumber = covidtest.Idnumber
order by CovidTestDate desc
limit 1
) covidtest on 1=1
Hi i have a problem of how to output the latest data of "reading_timesent" by using max(Date), And Also Include the other column where the latest date, and remove redundant data using distinct or group i don't know whats my problem here's the sample img and data
Patient table
Reading Table
Inner Join table
Expected Output
JSFIDDLE SAMPLE
SELECT * FROM Patients P
INNER JOIN Reading R ON R.patient_ID = P.patient_ID
Try this
SELECT * FROM
(
SELECT *,
(
SELECT reading_ID
FROM Reading
WHERE Reading.patient_ID = p.patient_ID
ORDER BY reading_timesent DESC
LIMIT 1
) AS newestReadingID
FROM Patients p
) AS subquery
JOIN Reading ON subquery.newestReadingID = Reading.reading_ID
You can use a correlated subquery for a condition in the ON clause:
SELECT *
FROM Patients P
INNER JOIN Reading R
ON R.patient_ID = P.patient_ID
AND R.reading_timesent = (
SELECT MAX(R2.reading_timesent)
FROM Reading R2
WHERE R2.patient_ID = P.patient_ID
)
Another way:
SELECT P.*, R.*
FROM (
SELECT patient_ID, MAX(reading_timesent) as reading_timesent
FROM Reading
GROUP BY patient_ID
) X
JOIN Patients P USING (patient_ID)
JOIN Reading R USING (patient_ID, reading_timesent)
Usually I would use the AUTO_INCREMENT column instead of a TIMESTAMP column for similar tasks. But that is not always applicable.
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;
Please I need to figure out what I am doing wrong. I created this inner join code for mysql. it works but it gives me repeated values like repeating a particular row twice or categoryid twice. each of the tables(users,paymentnotification,monthlyreturns) has the categoryid used to check and display the username(users.pname) from the user table, then check and display those that have made payment from the monthly returns and payment table using the categoryid.
$r="SELECT monthlyreturns.categoryid, monthlyreturns.month, monthlyreturns.quarter, monthlyreturns.year,paymentnotification.amount, users.pname, monthlyreturns.ototal, paymentnotification.payee, status
FROM paymentnotification
INNER JOIN (monthlyreturns INNER JOIN users ON monthlyreturns.categoryid=users.categoryid)
ON monthlyreturns.categoryid=paymentnotification.categoryid
ORDER BY monthlyreturns.categoryid DESC";
I think the query you want is more like this:
SELECT b.categoryid, b.month, b.quarter, b.year, a.amount, c.pname, b.ototal, a.payee, status
FROM paymentnotification a
INNER JOIN monthlyreturns b
ON a.categoryid = b.categoryid
INNER JOIN users c
ON b.categoryid = c.categoryid
ORDER BY b.categoryid DESC
The way you are doing the correlations doesn't seem clear and may cause problems. Try this one out and see what happens. If its still doing duplicates, perhaps the nature of the data require further filtering.
Assuming I understand what you're trying to do, you are not joining your tables properly. Try joining one at a time
SELECT DISTINCT monthlyreturns.categoryid, monthlyreturns.month, monthlyreturns.quarter, monthlyreturns.year,paym entnotification.amount, users.pname, monthlyreturns.ototal, paymentnotification.payee, status
FROM paymentnotification
INNER JOIN monthlyreturns
ON paymentnotification.categoryid = monthlyreturns.categoryid
INNER JOIN users
ON monthlyreturns.categoryid = users.categoryid
ORDER BY monthlyreturns.categoryid DESC
I don't see any problem.. I get 4 result rows: check this fiddle http://sqlfiddle.com/#!2/165a22/5
this is the query I used:
SELECT m.categoryid, m.month, m.quarter, m.year,p.amount, u.pname, m.ototal, p.payee, m.status
FROM paymentnotification p JOIN monthlyreturns m ON p.categoryid = m.categoryid
JOIN users u ON u.categoryid = m.categoryid
ORDER BY m.categoryid DESC
there are no duplicated rows, just "unique" rows if you consider every column you choose.
Hope it helps
SELECT M.categoryid, M.month, M.quarter, M.year, M.ototal,
P.amount, P.payee, P.status,
U.pname
FROM paymentnotification AS P
INNER JOIN monthlyreturns AS M ON P.categoryid = M.categoryid
INNER JOIN users AS U ON M.categoryid = U.categoryid
ORDER BY M.categoryid DESC
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.