Relational algebra - select (select) - sql

Suppose I have a relation map called M with from and to.
I want to find all rooms that can be accessed with 1 intermediate room. Would the following lines be correct?
P = πTo (σFrom='Vestibule'(M))
X = P U πTo (σFrom=P(M))
X = πTo (σFrom= (σFrom='Vestibule'(M)) (M))
In other words, can I use the outcome of a select in another select, or do I have to use a product?
Thanks in advance.

if i understand right, you need a inner query or nested queries into inside
following link explains logic of inner and outer queries it may help you.
http://www.codecandle.com/articles/sql/subqueries/sql-subquery.html

Related

How to only select an attribute one time

This code shows me the results based on two tables
SELECT h.Destino, g.Fuente
FROM Hist_LDS h, gTronco g
WHERE h.Fecha='2020-10-28'
AND g.Tronco = h.Tronco
The Destinos are 19 and then it repeat, i need to only apeers one time, and count the time od appereance to. I need to create a new table when insted of the information in the variable Tronco I put the Destino and Fuente
What you are doing is a CROSS JOIN.
And what you probably want is a simple INNER JOIN. Not very clear from your question, also check LEFT/RIGHT JOIN.
SELECT h.Destino, g.Fuente
FROM Hist_LDS h
INNER JOIN gTronco g ON g.Tronco = h.Tronco
WHERE h.Fecha='2020-10-28'

SQL join 4 tables with WHERE query

I am trying to join 4 tables together as shown in this diagram here:
https://imgur.com/a/jukJvSw
The SQL query I have written returns all fields except TExpiryDate and I have not come across any examples online that can help me understand this. Please help.
SELECT tbPurchaseHeader.PurchaseDate,
tbSupplier.CompanyName,
tbPurchaseDetails.UnitCost,
tbPurchaseDetails.Quantity,
tbPurchaseDetails.Bonus,
tbpurchasedetails.BatchID,
tbBatch.TExpiryDate
FROM ((tbPurchaseDetails
INNER JOIN tbPurchaseHeader
ON tbPurchaseDetails.PurchaseID = tbPurchaseHeader.PurchaseID)
LEFT JOIN tbBatch
ON tbPurchaseDetails.BID = tbBatch.BID)
INNER JOIN tbSupplier
ON tbPurchaseHeader.SupplierID = tbSupplier.SupplierID
WHERE tbPurchaseDetails.ProductID = ?
ORDER BY tbPurchaseHeader.PurchaseDate
Turns out BID contains empty values in the database. I have decided to make links elsewhere to get the data. Thanks everyone for making me realise this.

SQL combining COUNT with NO COUNT

I have a practice question that I am struggling with.
Here are:
image of the table
my code and the answer i get
the answer im SUPPOSED to get
Thanks.
As far as I understand, you get the TeachersAnswers result from the query.
I think that you may use the below query instead of yours :
SELECT horse_name, COUNT(place)
from horse h
left join entry e on e.horse_id = h.horse_id
where e.place = 1
group by horse_name

SQL to query drupal nodes with multiple taxonomies

Good morning all.
I've read some of the suggested question before posting this but seems like no one has my same issue (probably and inidicator of how bad I am in Drupal and coding in general)
I need to write a query that returns all the nodes with TWO SPECIFIC taxonomies associated to it (of which I know the IDs), but it seems I don't know the right syntax cause I just manage to get it work with ONE term id.
Here's what I have so far (and works)
SELECT * FROM node
INNER JOIN term_node AS tn ON node.vid = tn.vid
LEFT JOIN content_type_extra_content AS xc ON node.vid = xc.vid
WHERE tn.tid IN (SELECT th.tid FROM term_hierarchy AS th WHERE th.tid = '146')
That '146' is the id of the first taxonomy term I need to check (call it "shoes")
Now I have to check that the node has also the taxonomy id '223' (call it "season")
I've tried different solutions with no avail.
I'm pretty sure the solution is under my nose but at the moment I can't wrap my head around it.
Please note that the taxonomies are in different vocaboularies and they are at level-0
Thanks in advance for any help
If I understand you correctly, you want the nodes which have 2 specific terms (shoes and season), then try something like this:
SELECT * FROM node
INNER JOIN term_node AS tn ON node.vid = tn.vid
LEFT JOIN content_type_extra_content AS xc ON node.vid = xc.vid
WHERE tn.tid IN ('146','223')
GROUP BY node.vid
HAVING count(*) = 2

sql query question inner join

LEFT JOIN PatientClinics AB ON PPhy.PatientID = AB.PatientID
JOIN Clinics CL ON CL.ID = AB.ClinicID
AND COUNT(AB.ClinicID) = 1
I get error using Count(AB.ClinicID) = 1 (ClinicID has duplicate values in the table and
I want to use only 1 value of each duplicate value of ClinicId to produce result)
What mistake am I making?
I've never seen a COUNT() being used in a JOIN before. Maybe you should use:
HAVING COUNT(AB.ClinicID) = 1
instead.
Count() can't be used as a join/filter predicate. It can be used in the HAVING clause however. You should include the entire query in order to get a better example of how to rewrite it.
maybe investigate the HAVING clause instead of using COUNT where you put it.
hard to help without the full query.