Hi Order By not working on MySql
the code is as follows,
select * from School where School.type = 'HighSchool'
order by (select locations.name from locations inner join School_locations
on locations.id = School_locations.location_id where
School_locations.School_id = School.id and locations.location_country = 'US' limit 1)
and the output is displaying same for both ascending as well as descending how to solve this problem
I don't think you'd need to do the subquery:
SELECT s.*
FROM School s
INNER JOIN School_locations sl ON (s.id = sl.School_id)
INNER JOIN locations l ON (l.id = sl.location_id)
WHERE l.location_country = 'US' AND s.type = 'High school'
ORDER BY l.name
select school.* from school
inner join school_locations on school_locations.schoolid = school.school_id
inner join locations on locations.location.id = school_locations.locationid
where
locations.location_country = 'US' and school.type = 'HighSchool'
order by
locations.name
limit 1
you can use this query
select School.* from School inner join School_locations
on School_locations.School_id = School.id
inner join locations
on locations.id = School_locations.location_id
where locations.location_country = 'US' and School.type = 'HighSchool'
order by locations.name limit 1
Related
I have problem that I hope someone can help me with.
SELECT a.country_name, s.state_name, c.city_id,
LEAST (c.next_1, c.next_2, c.next_3) AS next_visit,
MAX(v.visit_time) AS last_visit
FROM city c
INNER JOIN country a ON a.id = c.country
INNER JOIN state s ON s.id = c.state
INNER JOIN visit_log v ON CONCAT(c.country, c.state, c.city_id) = CONCAT(v.country, v.state, v.city_id)
GROUP BY CONCAT(v.country, v.state, v.city_id)
ORDER BY a.id ASC, s.id ASC, c.city_id
My main problem now is that I can't get the col_1 and col_2 from the visit_log corresponding with MAX(visit_log)
SQLfiddle
You can add the "latest" requirement to the join condition:
SELECT *
FROM city c
JOIN country a
ON a.id = c.country
JOIN state s
ON s.id = c.state
JOIN visit_log v
ON v.country = c.country
AND v.state = c.state
AND v.city_id = c.city_id
AND visit_time =
(
SELECT MAX(visit_time)
FROM visit_log v2
WHERE v2.country = c.country
AND v2.state = c.state
AND v2.city_id = c.city_id
)
You can find many other approaches in the greatest-n-per-group+mysql tag. For optimal speed you'd use an approach using variables.
You can try this:-
SELECT C.NAME, S.NAME, CN.ID, NV.Next_visit_1, VL.visited
FROM COUNTRY C INNER JOIN next_visit NV ON C.ID = NV.Country
INNER JOIN STATE S ON NV.State = S.ID
JOIN CITY
INNER JOIN visitor_log VL ON CONCAT(NV.country, NV.state, NV.city) = CONCAT(VL.country, VL.state, VL.city)
I want to select a "site" record(s) randomly, and then get the related "channels" for it. Here's what I've tried that doesn't work. Please help. Thank you!
SELECT A.*, B.*
FROM (
SELECT companies.company_name, sites.id
FROM sites
INNER JOIN company_sites ON company_sites.site_id = sites.id
INNER JOIN companies ON companies.id = company_sites.company_id
WHERE sites.active = 1
AND sites.stage_id = 5
GROUP BY sites.id
ORDER BY RAND()
LIMIT 1
)A
JOIN (
SELECT
channels.id
FROM channels
WHERE channels.site_id = A.sites_id
) B ON 1 = 1
Try this:
SELECT just the fields you need
FROM (
SELECT companies.company_name, sites.id id
FROM sites
INNER JOIN company_sites ON company_sites.site_id = sites.id
INNER JOIN companies ON companies.id = company_sites.company_id
WHERE sites.active = 1
AND sites.stage_id = 5
GROUP BY sites.id
ORDER BY RAND()
LIMIT 1
)A
join channels on channels.site_id = id
By the way, if you just wanted the list of channels on each site, you can concatenate them into one field (rather than getting them on separate rows). The query would be simpler:
SELECT companies.company_name, sites.id, group_concat(channels.id) as channels
FROM sites
INNER JOIN company_sites ON company_sites.site_id = sites.id
INNER JOIN companies ON companies.id = company_sites.company_id
inner join channels on channels.site_id = sites.id
WHERE sites.active = 1
AND sites.stage_id = 5
GROUP BY sites.id
ORDER BY RAND()
LIMIT 1
$q = "SELECT s.id, s.title, s.description,
(SELECT COUNT(*) FROM ".FORUM_THREADS." t WHERE t.cat_id = s.id) AS topics,
(SELECT COUNT(*) FROM ".FORUM_REPLIES." r INNER JOIN ".FORUM_THREADS." t ON r.thread_id = t.id
WHERE t.cat_id = s.id) AS replies,
(SELECT r.date FROM ".FORUM_REPLIES." r INNER JOIN ".FORUM_THREADS." t ON r.thread_id = t.id
WHERE t.cat_id = s.id ORDER BY r.date DESC LIMIT 1) AS last_post
FROM ".FORUM_SUBCATEGORIES." s WHERE s.parent = '$catid' AND s.status = '0' ORDER BY s.id";
I am attempting to select more than one field on the following part of the query
(SELECT r.date FROM ".FORUM_REPLIES." r INNER JOIN ".FORUM_THREADS." t ON r.thread_id = t.id
INNER JOIN ".TBL_USERS." u ON u.id = r.author WHERE t.cat_id = s.id ORDER BY r.date DESC LIMIT 1) AS last_post
Along with r.date, I want to select u.username and r.author.
How can I go about doing this?
Thanks!
Just add them to the SELECT:
(SELECT r.date, r.author, u.username FROM ".FORUM_REPLIES." r INNER JOIN ".FORUM_THREADS." t ON r.thread_id = t.id
INNER JOIN ".TBL_USERS." u ON u.id = r.author WHERE t.cat_id = s.id ORDER BY r.date DESC LIMIT 1) AS last_post
UPDATED after comment from OP:
You need to do 3 separate selects OR (depending on your data model) change the query so that the last_post query ends up after/in the FROM clause (there it can have as many columns as you want)...
Luke, you have a central select statement which uses nested select statements for getting the count. You can't depend on the nested select statements to count as the inner join, so you're going to have to add them to the central select statement instead.
In other words, join ".FORUM_REPLIES." and "u" (not sure what that's supposed to represent) with ".FORUM_SUBCATEGORIES.". I'd write the query for you, but I don't know how to link subcategories with replies and subcategories with u.
I have this SQL that works fine although a little slow. Ideally would like to have the extra sub query in the where clause something like this AND country_iso = "uk".
SELECT c.content_name, c.content_telephone, c.content_address1
(SELECT w.weblink_url FROM weblinks w WHERE w.content_id = c.content_id ORDER BY w.weblink_ordering, w.weblink_id ASC LIMIT 1)
AS content_url,
(SELECT country_name FROM countries WHERE country_id = c.country_id LIMIT 1)
AS country_name,
(SELECT country_iso FROM countries WHERE country_id = c.country_id LIMIT 1)
AS country_iso
FROM catcontents cc
LEFT JOIN content c ON c.content_id = cc.content_id
WHERE cc.category_id = 7
AND (SELECT country_iso FROM countries WHERE country_id = c.country_id LIMIT 1) = 'uk'
ORDER BY cc.catcontent_ordering ASC, c.content_name ASC
Would this query not suit your needs? (basically, use joins to get the country, instead of sub-selects.)
SELECT
c.content_name,
c.content_telephone,
c.content_address1,
countries.country_name,
countries.country_iso,
(
SELECT w.weblink_url
FROM weblinks w
WHERE w.content_id = c.content_id
ORDER BY w.weblink_ordering, w.weblink_id ASC
LIMIT 1
) content_url
FROM catcontents cc
LEFT JOIN content c ON c.content_id = cc.content_id
JOIN countries ON countries.country_id = c.country_id
WHERE cc.category_id = 7
AND countries.country_iso = 'uk'
ORDER BY cc.catcontent_ordering ASC, c.content_name ASC
SELECT c.content_name,
c.content_telephone,
c.content_address1,
w.weblink_url content_url,
country_name,
country_iso
FROM catcontents cc
LEFT JOIN content c ON c.content_id = cc.content_id
LEFT JOIN weblink_url w ON w.content_id = c.content_id
JOIN countries ON country_id = c.country_id
AND country_iso = 'uk'
WHERE cc.category_id = 7
select
s.id, s.description, s.improvement, s.previous_year_id,
s.current_year_id, s.first_name, s.last_name, s.username,
s.finding, s.action, s.share, s.learned, s.timestamp,
d.title as department_title,
group_concat(g.title SEPARATOR \' | \') as strategic_goals,
y1.year as current_year_title, y2.year as previous_year_title,
u.summary_id, u.file_name as file_name
from
summary s, year y1, year y2, strategic_goal_entries sge,
goal g, department d, uploads u
where
s.id = sge.summary_id
and
s.current_year_id = y1.id
and
s.previous_year_id = y2.id
and
sge.goal_id = g.id
and
s.id = u.summary_id
and
s.department_id = d.id
and
s.department_id = '4'
group by
s.id
This only returns records from the summary table that has a relating record in the uploads table (s.id = uploads.summary_id) that contain a value within the uploads.summary_id field
I want to return all records, whether or not it has a file associated with it.
Any help is appreciated.
Suggest refactoring this SQL query to use ANSI joins. To achive your goal, you'd want a LEFT JOIN instead:
SELECT /*your columns*/
from summary s
INNER JOIN year y1 ON s.current_year_id = y1.id
INNER JOIN year y2 ON s.previous_year_id = y2.id
INNER JOIN strategic_goal_entries sge ON s.id = sge.summary_id
INNER JOIN goal g ON sge.goal_id = g.id
INNER JOIN department d ON s.department_id = d.id
LEFT JOIN uploads u ON s.id = u.summary_id
WHERE s.department_id = '4'
group by s.id