mySQL: Get records union from same table - mysql

I have 2 tables a car model and car parts. Both are related with a partfitment table whose only fields are the primary keys modelID and partID.
I am trying to display the parts that fit the model car I selected.
The following works fine:
SELECT tblmodel.modelID,
tblmodel.model,
tblparts.part,
tblparts.part_number,
tblparts.description,
tblparts.list_price
FROM (tblmodel
INNER JOIN tblpartfitment
ON tblmodel.modelID = tblpartfitment.modelID)
INNER JOIN tblparts
ON tblpartfitment.partID = tblparts.partID
WHERE tblmodel.modelID = 1;
The tblparts table has the tinyint field named universal_part. I am trying to include all the parts that have universal_part = true in my original query.
In other words always return all parts that are universal and those that are for the modelID specified.
I tried using a union, but get errors.

I think you can just include the logic in the ON clause:
SELECT m.ModelID, m.Model, p.part, p.part_number, p.description, p.list_price
FROM tblmodel m INNER JOIN
tblpartfitment pf
ON m.ModelID = pf.modelID INNER JOIN
tblparts p
ON pf.partID = p.partID OR p.universal_part = 1
WHERE m.ModelID = 1;
EDIT:
You may be best off using union:
SELECT m.ModelID, m.Model, p.part, p.part_number, p.description, p.list_price
FROM tblmodel m INNER JOIN
tblpartfitment pf
ON m.ModelID = pf.modelID INNER JOIN
tblparts p
ON pf.partID = p.partID AND p.universal_part <> 1
WHERE m.ModelID = 1
UNION ALL
SELECT m.ModelID, m.Model, p.part, p.part_number, p.description, p.list_price
FROM tblmodel m CROSS JOIN
tblparts p
WHERE p.universal_part = 1;

Related

Why the output values are repeating? what should be the right query?

This is the query i wrote, but when i execute the query the values are repeating. so help me to write the right query
SELECT p.id,
p.NAME,
p.year,
p.address,
p.caste,
p.landextent,
p.adharno,
p.drillingdate,
p.pumpseterectiondate,
p.pumpsethp,
p.surveyno,
p.registrationdateinescom,
p.ymdmsdpaid,
p.ymdpaiddate,
p.energisationno,
p.energisationdate,
p.mobile,
p.remarks,
c.constituency_name constituency,
t.NAME taluka,
e.NAME escom,
d.district_name district,
division.divison_name division,
p.crsubmitted,
p.uniqueid,
p.yearofdrilling,
p.yearofpumpset,
p.yearofregistration,
p.yearofenergisation,
p.escomdivuseractive
FROM progress p
INNER JOIN constituency c
ON p.constituency_id = c.id
INNER JOIN taluka t
ON c.taluka_id = t.id
INNER JOIN district d
ON t.district_id = d.id
INNER JOIN divison di
ON d.divison_id = di.id
INNER JOIN divisons division
ON d.divisons_id = division.id
INNER JOIN escomdivison e
ON e.district_id = d.id
WHERE di.id = 3;
I think you need to remove the line
INNER JOIN divisons division on d.divisons_id=division.id
since you already have division table written on the one line above for the INNER JOIN conditions, and those(alias and table) are confused among them.

Extracting latest version of articles from the results of a previous query

I have the following query:
SELECT e_c.*, c.name, j.status, j.version, j.articleId, j.title FROM assetcategory AS c
INNER JOIN assetentries_assetcategories AS e_c
ON c.categoryId = e_c.categoryId AND c.name = 'news'
INNER JOIN assetentry AS e
ON e.entryId = e_c.entryId
INNER JOIN journalarticle AS j
ON j.resourcePrimKey = e.classPK
AND e.classNameId = (SELECT classNameId FROM classname_ WHERE value = 'com.liferay.portlet.journal.model.JournalArticle')
AND j.companyId= e.companyId
WHERE j.status = 0
which returns all the category news in the journalarticles. From the results I need to select the most recent versions for each articleId. For example suppose there is an article with 4 versions, even with different title, it is the same article because it will have the same articleId. So therefore for each unique articleId I need the latest version. How can I do that?
Add a join to a subquery which finds the most recent version for each article:
SELECT e_c.*, c.name, j1.status, j1.version, j1.articleId, j1.title
FROM assetcategory AS c
INNER JOIN assetentries_assetcategories AS e_c
ON c.categoryId = e_c.categoryId AND c.name = 'news'
INNER JOIN assetentry AS e
ON e.entryId = e_c.entryId
INNER JOIN journalarticle AS j1
ON j1.resourcePrimKey = e.classPK AND
e.classNameId = (SELECT classNameId FROM classname_
WHERE value = 'com.liferay.portlet.journal.model.JournalArticle') AND
j.companyId = e.companyId
INNER JOIN
(
SELECT articleId, MAX(version) AS max_version
FROM journalarticle
WHERE status = 0
GROUP BY articleId
) j2
ON j1.articleId = j2.articleId AND j1.version = j2.max_version;
The basic idea behind the join to the subquery aliased as j2 above is that it restricts the result set to only the most recent version of each article. We don't necessarily have to change the rest of the query.

NEGATION INNER JOIN with WHERE

Why the sql code below ignores
WHERE r.tt_schedules_id = '105'
in
SELECT DISTINCT r.load_date, u.url
FROM tt_results r
INNER JOIN url_lp u
ON u.lp_id <> r.lp_id
INNER JOIN tt_schedules s
ON s.tt_schedules_id = r.tt_schedules_id
WHERE r.tt_schedules_id = '105'
AND (DATE(NOW()) - DATE(r.load_date) <= 7)
?
Basically, it returns everything with any
tt_schedules_id
There are 5 tables:
url_lp, tt_results (table having foreign keys in url_lp, tt_schedules and
tt_tags), tt_schedules, tt_tags(not used) and tt_schedules_url_lp_hub
If you draw the scheme on the paper you should be clear.
I fixed it:
SELECT u.url FROM url_lp u
INNER JOIN tt_schedules_url_lp_hub hub
ON hub.lp_id = u.lp_id
INNER JOIN tt_schedules s
ON hub.tt_schedules_id = s.tt_schedules_id
WHERE s.tt_schedules_id = '105'
AND u.lp_id NOT IN
(SELECT r.lp_id FROM tt_results r WHERE r.tt_schedules_id = '105')
Cheers.

slow query with joins

Please am having difficulty in optimizing this query. What am trying to achieve is to join about 8 tables, of which only about 3 of the tables contains large data (1.5m records). This query returns expected records but is taking 1min to run which is bad.
I know it can be optimized to perform far better, pls i need assistance from you experts. I have index on the fields used for join already.
SELECT topic_id,
topic_title,
unit_name_abbrev,
sch_name_abbrev,
picture_small_url AS thumbnail,
profile_pix_upload_path,
first_name,
last_name,
topic_poster,
topic_replies,
topic_views,
topic_last_post_time AS topic_post_time,
sch_sub_forum_id
FROM (_sch_forum_topics
INNER JOIN _users
ON ( _users.userid = _sch_forum_topics.topic_poster )
INNER JOIN _profile
ON _profile.userid = _users.userid
INNER JOIN _class
ON _users.classid = _class.classid
INNER JOIN _level
ON _class.level_id = _level.id
INNER JOIN _unit
ON _class.unitid = _unit.unitid
INNER JOIN _department
ON _unit.deptid = _department.deptid
INNER JOIN _faculty
ON _department.facid = _faculty.facid
INNER JOIN _university
ON _faculty.schid = _university.schid)
WHERE _sch_forum_topics.sch_sub_forum_id = 4
ORDER BY _sch_forum_topics.topic_last_post_time DESC
LIMIT 0, 15
Try to filter before making JOIN's.
SELECT topic_id,
topic_title,
unit_name_abbrev,
sch_name_abbrev,
picture_small_url AS thumbnail,
profile_pix_upload_path,
first_name,
last_name,
topic_poster,
topic_replies,
topic_views,
topic_last_post_time AS topic_post_time,
sch_sub_forum_id
FROM
( select * FROM sch_forum_topics WHERE sch_sub_forum_id = 4
ORDER BY topic_last_post_time DESC
LIMIT 0, 15 ) main
INNER JOIN _users
ON ( _users.userid = main.topic_poster )
INNER JOIN _profile
ON _profile.userid = _users.userid
INNER JOIN _class
ON _users.classid = _class.classid
INNER JOIN _level
ON _class.level_id = _level.id
INNER JOIN _unit
ON _class.unitid = _unit.unitid
INNER JOIN _department
ON _unit.deptid = _department.deptid
INNER JOIN _faculty
ON _department.facid = _faculty.facid
INNER JOIN _university
ON _faculty.schid = _university.schid);

How to make SQL query from many tables on Rails?

Thank you for read my quetsion.
I want to know which is the best or correct way to express queries in Rails which make many joins and select attributes from multiple tables:
SELECT
ffca.name,
ffc.name,
ft.title,
concat(s.first_name, ' ', s.last_name),
pf.name,
ft.amount,
date_format(ft.created_at,'%d/%m/%y')
FROM
finance_transactions ft
INNER JOIN students s ON ft.student_id = s.id
INNER JOIN payment_forms pf ON ft.payment_form_id = pf.id
INNER JOIN finance_fees ff ON ft.finance_fees_id = ff.id
INNER JOIN finance_fee_collections ffc ON ff.fee_collection_id = ffc.id
INNER JOIN finance_fee_categories ffca ON ffc.fee_category_id = ffca.id;
If you need extra info, please feel free to ask me.
Thank you very much.
e.g
your model is finance_transactions
finace_transaction = FinanceTransaction.find_by_sql(
"SELECT
ffca.name as name,
ffc.name as name1,
ft.title as title,
concat(s.first_name, ' ', s.last_name) as full_name,
pf.name as pf_name,
ft.amount as count,
date_format(ft.created_at,'%d/%m/%y') as time
FROM
finance_transactions ft
INNER JOIN students s ON ft.student_id = s.id
INNER JOIN payment_forms pf ON ft.payment_form_id = pf.id
INNER JOIN finance_fees ff ON ft.finance_fees_id = ff.id
INNER JOIN finance_fee_collections ffc ON ff.fee_collection_id = ffc.id
INNER JOIN finance_fee_categories ffca ON ffc.fee_category_id = ffca.id"
)
the table finance_transactions table must have column name, name1,title, full_name,pf_name, count, time
and then you can use:
finace_transaction.name1 gets ffc.name
You might want to provide a method in your model class as a wrapper
class FinanceTransaction < ActiveRecord::Base
[...]
def self.find_fee_transactions
self.find_by_sql(<<-SQL)
SELECT
ffca.name as name,
ffc.name as name1,
ft.title as title,
concat(s.first_name, ' ', s.last_name) as full_name,
pf.name as pf_name,
ft.amount as count,
date_format(ft.created_at,'%d/%m/%y') as time
FROM
finance_transactions ft
INNER JOIN students s ON ft.student_id = s.id
INNER JOIN payment_forms pf ON ft.payment_form_id = pf.id
INNER JOIN finance_fees ff ON ft.finance_fees_id = ff.id
INNER JOIN finance_fee_collections ffc ON ff.fee_collection_id = ffc.id
INNER JOIN finance_fee_categories ffca ON ffc.fee_category_id = ffca.id"
)
SQL
end
end