I'm new to SQL, I have 3 tables in my database:
One named notifications,
the other notifications_log,
and the third one is control.
Both notification and notification_log have notification_id with pk-fk relation.
There is also another column named control_id in notifications and I have this column in control table too.
Now what I want to do is to get rows under description column of notification_log table by connecting notification table and control table with their control_id field. Can you help me with that?
Here's something I've tried:
select c.control_name
from notifications note, notifications_log note_log, control c
where note_log.ALARM_ID = note.ALARM_ID
and note.CONTROL_ID = C.CONTROL_ID
order by control_name desc
Use JOIN:
SELECT c.control_name, note_log.description FROM notifications note
INNER JOIN notifications_log note_log
ON note.notification_id = note_log.notification_id
INNER JOIN control c
ON note.control_id = c.control_id
ORDER BY c.control_name DESC
What you have to understand is if every record in control table has a corresponding record in other tables, otherwise it will not be showed with INNER JOIN.
select description from notification_log where notification_id in (select notification_id from notification)
if you want to get only the description..
hope it helps :D
From your description, it sounds as though you are selecting the wrong field in your query. Try:
select distinct note_log.description
from notifications note, notifications_log note_log, control c
where note_log.notification_id = note.notification_id
and note.CONTROL_ID = C.CONTROL_ID
order by 1
Related
Hi guys I'm new with databases and I'm trying to make a query where I join 3 tables. I could make it and I want to clean up the result. I want to know how can I delete the column "pin" from users table and maybe some "ids" columns.
Select * from "wish-list"
Join products
On "wish-list".id = products.holiday_id
Join users
On "wish-list".user_id = users.id
Where "wish-list".id = 1
You need to specify which columns you really need in your output. At the moment you are using
SELECT * which outputs all columns of all joined tables.
Here is what it should look like:
SELECT holiday, products.description, users.pin FROM "wish-list"
JOIN products ON "wish-list".id = products.holiday_id
JOIN users ON "wish-list".user_id = users.id
WHERE "wish-list".id = 1
It's important that you reference all columns which are not your main entity (here wish-list) with tablename.column (products.description and not only description). It will work without referencing strictly but only if the column name is unique in your query.
Furthermore you can rename columns. This is useful for example if you want to get the id's of the product table and the wish-list table.
SELECT product.id AS product_id, id AS wishlist_id FROM "wish-list"
...
Hope that helps!
I read this FAQ http://www.firebirdfaq.org/faq289/ and I want to use select to count detail record from current record so I tried this
((
select count(*) from detail_table where detail_table.id = id
))
But I did not get correct values I got numbers like 19 where in fact it is the total number of records in detail_table! How can I get only the count of records related to current master record in master table?
The problem is that id refers to the id column of detail_table and not of the master table. So it is the equivalent of:
select count(*) from detail_table where detail_table.id = detail_table.id
Which means you are simply counting all rows. Instead - assuming the master table is master_table - you should use:
select count(*) from detail_table where detail_table.id = master_table.id
Note that, as also mentioned in the FAQ you link to, you should really consider using a view instead of a computed column when referencing other tables as it is not very good for performance.
The view equivalent would be something like
CREATE OR ALTER VIEW master_with_detail_count
AS
SELECT master_table.id, coalesce(c.detail_count, 0) as detail_count
FROM master_table
LEFT JOIN (SELECT id, count(*) as detail_count FROM detail GROUP BY id) c
ON c.id = master.id
I need to query 3 tables from a database.
The table Client includes client id no. and client name.
The table Property includes property id no. and property name amongst other things.
The table clientsInterestedInProperties includes client id no. property id no. and date of visit to property.
I want to list the name of clients who have an interest in a specific property (with the name, not id. no) and the date they visited the property.
For example say the property is called Barker Hall, who is interested in it and when did they visit?
Can anyone help?
First, use main filtering condition in your from table. So you must start with
select ... from Property ... ,
Then connect visitations to the property. It will generate different row for each visit, with
LEFT JOIN to get visitation to the property
Then we need to get readably client names, not IDs, so we do
LEFT JOIN to get client names
And some additional filtering/conditions can be added with
WHERE clauses ( or INNER JOIN ) , like visiting period.
Example SQL for your case ( i don't know your table/column names )
select PropertyName, ClientsInProp_VisitDate, ClientName
from Property
left join ClientsInProp on ClientsInProp_Propertyid = Property_PropertyID
left join Client on Client_Clientid = ClientsInProp_ClientId
where Proprty_Name = 'House 1' and ClientsInProp_VisitDate > '01.10.2013'
Cheers !
What you want is an IN clause:
SELECT * FROM Client
WHERE ClientId IN
(SELECT ClientID
FROM clientsInterestedInProperties cip
INNER JOIN Property p
ON cip.PropertyID = p.PropertyID
WHERE p.ProperyName = #propertyName)
I have two related tables in SQL Server in a 1 to many relationship (Applicant, Reference).
I want a view that will retrieve all the data from the Applicant table and also add another column to the view that tells me how many related Reference rows there are where the "Complete" column is True.
Something like this using a subquery:
select applicantfield1, applicantfield2,
(select count(*) from
reference where reference.applicantkey = applicant.applicantkey
and reference.complete = 1) AS referencecount
from applicant
Unless the complete field is in the applicant table (not the reference table). If so, it would be more like this:
select applicantfield1, applicantfield2,
(select count(*) from
reference where
reference.applicantkey = applicant.applicantkey) AS referencecount
from applicant
where applicant.complete = 1
Something like the below. You need to join on a table. This will handle where the applicant also doesn't have a True reference.
SELECT A.*, isnull(r.comptotal,0) as CompleteTotal
FROM Applicant as a Left Join
(SELECT ApplicantId, Count(Complete) as comptotal
FROM Reference Where Complete=1 Group by ApplicantID) as r
on a.ApplicantId = r.applicantId
i hava a set of following tables
customer(cus_id,cus_name);
jointAccount(cus_id,acc_number,relationship);
account(acc_number,cus_id)
now i want to create a select statement to list all the jointAccounts,
it should included the both customer name, and relationship.
I have no idea how to retrieve both different user name, is that possible to do this?
Generally speaking, yes. I'm assuming you mean you want to get customer info for both sides of the joint account per your jointAccount table. Not sure what database you're using so this answer is assuming MySQL.
You can join on the same table twice in a single SQL query. I'm assuming you have not yet created your tables, as you have cus_id listed twice in the jointAccount table. Typically these would be something like cus_id1 and cus_id2, which I've used in my sample query below.
Example:
SELECT c1.cus_id AS cust1_id, c1.cus_name AS cust1_name
, c2.cus_id AS cust2_id, c2.cus_name AS cust2_name, j.relationship
FROM customer c1
INNER JOIN jointAccount j
ON c1.cus_id = j.cus_id1
, customer c2
INNER JOIN jointAccount j
ON c2.cus_id = j.cus_id2
I haven't tested this but that's the general idea.
try this query:
SELECT * FROM jointAccount a LEFT JOIN customer c ON a.cus_id = c.cus_id;
just replace the * with the name of the columns you need.