This query works. But is there are better way to constructed it? I thought INNER Joins might be used. But I don't think I require them.
SELECT site.Name, vuln.Risk
from site, vuln, system
WHERE vuln.sysID=system.ID AND system.siteID=site.ID;
Yes, You can use JOIN, e.g.:
SELECT s.name, v.risk
FROM site s JOIN system sys ON s.id = sys.siteID
JOIN vuln v ON v.sysID = s.ID;
You can use explicit join sintax (work in the same way but is more clear)
SELECT
site.Name
, vuln.Risk
FROM site
INNER JOIN system on system.siteID=site.ID
INNER JOIN vuln on vuln.sysID=system.ID;
I'd definitely recommend using explicit join syntax as they modern and clear.
select site.name,
vuln.Risk
from site
join system on vuln.sysID = system.ID
join vuln on system.siteID = site.ID
Related
How to convert the code below from Oracle 8i to MySQL-
select count(*)
from patient_visit,
organization_master
where patient_visit.organization_id=organization_master.organization_id(+);
In the where statement "organization_master.organization_id(+)" is not working in MySQL.
Pls suggest.
The (+) is an Oracle-specific notation for an outer join.
I think you should write something like this
select count(*)
FROM patient_visit
LEFT OUTER JOIN organization_master
ON patient_visit.organization_id=organization_master.organization_id
I haven't tested it since I have no data to test it but it should work.
Hope it helps
You would need to use the standard JOIN syntax which is supported by all modern SQL databases (also Oracle):
select count(*)
from patient_visit
left join organization_master
on patient_visit.organization_id = organization_master.organization_id
If you have other joins without the (+), then just replace those with inner join: avoid the comma completely in the from clause. Other, non-join conditions just stay in the where clause.
For example:
select count(*)
from patient_visit
inner join patient_registration
on patient_registration.pprn_regd_id = patient_visit.pprn_regd_id
left join organization_master
on patient_visit.com_organization_id = organization_master.com_organization_id
where patient_visit.ghm_hosp_id = i_hosp_id
I'm trying to study SQL.
I have a problem with JOIN
I want to display ref_id, pro_name, class_name but I couldn't.
I find EFFICIENT solution.
MY QUERY (DOESN'T WORK)
SELECT
ref_id, pro_name, class_name
FROM
RC, RP, PP, LP
WHERE
RC.ref_id = RP.ref_id
Avoid using commas be CROSS JOIN
You could use JOIN to instead of commas
like this.
SELECT
RP.ref_id, PP.pro_name, LP.class_name
FROM
RP
LEFT JOIN RC ON RC.ref_id = RP.ref_id
LEFT JOIN PP ON PP.pro_id = RP.pro_id
LEFT JOIN LP ON LP.lec_id = RP.lec_id
Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
You would seem to want:
select rp.pro_id, pp.pro_name, lp.class_name
from rp left join
pp
on rp.pro_id = pp.pro_id left join
lp
on rp.lec_id = lp.lec_id;
Note the use of left join. This ensure that all rows are in the result set, even when one or the other joins doesn't find a matching record.
From what I can see, the table rc is not needed to answer this specific question.
I got this, and I want to get their "company" names for each one.
SELECT `client`.`name`,`client`.`lastname`
FROM `check`,`reserv`,`client`
WHERE `check`.`idReserv`=`reserv`.`id`
AND `reserv`.`idPerson`=`client`.`id`
ORDER BY `check`.`id`
, and I want to get their "company" names for each one, from table "company".
So I tried this:
SELECT `client`.`name`,`client`.`lastname`, `company`.`name`
FROM `check`,`reserv`,`client`,`company`
WHERE `reserv`.`idCompany`=`company`.`id`
AND `check`.`idReserv`=`reserv`.`id`
AND `reserv`.`idPerson`=`client`.`id`
ORDER BY `check`.`id`
but there is some people in the table "reserv" with an "idCompany" inexistent. so with that condition, this query only show me people who has an existent "id" in the table "company". I want to show the people with no company up and the space of company.name in blank if there is no company
I tryed many ways even with joins, but I cannot fix it. I'm tired to write "company" also.
You can use LEFT JOIN for this purpose like-
reserv r LEFT JOIN company c ON r.idCompany = c.id
You should use LEFT join instead.
SQL LEFT JOIN
SELECT c.name, c.lastname, co.name
FROM check AS ck
LEFT JOIN reserv AS r ON(ck.idReserv = r.id)
LEFT JOIN client AS c ON(r.idPerson = c.id)
LEFT JOIN company AS co ON(r.idCompany = co.id)
ORDER BY c.id
The ANSI 89 standard uses , notation for table joins with the criteria of the join being in the where clause. However I don't believe mySQL supports this outer style of join needed to address your problem. To express an outer join in this syntax you would need to use a *= for left join or =* for a right join; but again not sure mySQL supports it.
So in your case:
SELECT `client`.`name`,`client`.`lastname`, `company`.`name`
FROM `check`,`reserv`,`client`,`company`
WHERE `reserv`.`idCompany`*=`company`.`id`
AND `check`.`idReserv`=`reserv`.`id`
AND `reserv`.`idPerson`=`client`.`id`
ORDER BY `check`.`id`
However, I find that notation difficult to read and no need for all the escaping of table/column names (except reserved words)... so the below follows the ANSI 92 standards which allow for the use of INNER and LEFT Join syntax to explicitly define the type of join. Both notations should optimize to the same execution plan so either works (provided mySQL supports the *= notation) as well; it's just a matter of which standard you choose to use.
SELECT client.name
, client.lastname
, company.name
FROM `check`
INNER JOIN reserv
on `check`.idReserv=reserv.id
INNER JOIN client
on reserv.idPerson=client.id
LEFT JOIN company
on reserv.idCompany=company.id
ORDER BY `check`.id
I have this query:
SELECT hit.timestamp,hit.id,config.Name,hit.meter_id,levels.LevelName, pos.sm_pos , hit.hit_value
FROM pos,hit,controllers,levels,config
WHERE hit.id=config.id
AND hit.meter_id=levels.id
AND pos.id=hit.id
AND pos.controller_id=controllers.id;
How to make an inner join query from this? With aliases or something? I was looking and I can't find anything for multiple table query.
The way you are joining tables is outdated now. It was used eartlier, now we use keyword like INNER/NATURAL/LEFT OUTER/RIGHT OUTER/CROSS etc. to join tables on basis of requirement.
Refer Join in Mysql
SELECT hit.timestamp,
hit.id,
config.Name,
hit.meter_id,
levels.LevelName,
pos.sm_pos,
hit.hit_value
FROM hit
INNER JOIN config
ON hit.id = config.id
INNER JOIN levels
ON hit.meter_id = levels.id
INNER JOIN POS
ON pos.id = hit.id
INNER JOIN controllers
ON pos.controller_id = controllers.id;
Note : The query posted by you is according the SQL-89 standard and the second posted by me is according to SQL-92.
The SQL-92 standard introduced INNER JOIN .. ON and OUTER JOIN .. ON in order to replace the more complex(?) syntax of SQL-89.
If you want to reformat your query, you can do it like this:
SELECT
H.timestamp,
H.id,
F.Name,
H.meter_id,
L.LevelName,
P.sm_pos,
H.hit_value
FROM pos AS P
INNER JOIN controllers AS C ON P.controller_id = C.id
INNER JOIN hit AS H ON P.id = H.id
INNER JOIN levels AS L ON H.meter_id = L.id
INNER JOIN config AS F ON H.id =F.id;
Notice that I've taken the liberty to add aliases on your table names, this can simplify your queries alot.
To understand how joins work in MySQL, read about it here in the manual. A good tutorial about joins was written by Jeff Atwood, you can find it here.
For these tables here (circled)
http://imgur.com/Q1HlJ
What would the best join be to use for them , I tried using OUTER join but it would not return any rows even if there was matching data.
Thanks
QUERY
select *
from
hr
OUTER JOIN
hr
ON
hr.procedure_id = procedure.procedure_id
OUTER JOIN
staff
ON
staff.staff_id = hr.staff_id
Where hr.procedure_id = procedure.procedure_id
I would suggest reading up on JOINs. It is hard to know what you are looking for. We won't make that decision for you.
What you probably want is this:
select p.*, hr.*, s.*
from procedure p
left outer join hr on p.procedure_id = hr.procedure_id
left outer join staff s on hr.staff_id = s.staff_id
That will give you results unless there are no rows in procedure.
There are different ways that you could put these together, though, depending on what exactly you want. I don't know what "hr" stands for here, so I can't use common sense to figure that out.
Don't bother with JOIN at all, and let the query optimizer handle it for you.
select * from hr, procedure, staff
where hr.procedure_id = procedure.procedure_id and staff.staff_id = hr.staff_id