How to make inner join on this query - mysql

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.

Related

how to join SQL with 3 tables

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.

MariaDB Query Construction

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

How do i do a join query in mysql on this scheme

I'm having some trouble trying to get all the data out of this database in one query.
Here is the Database scheme
I know that there is a way to do this with a join but i'm not sure how.
I need all the data from Questionnaire, Booking, and Customers.
I think i have to join on the Booking has customers but i'm not sure.
At the moment i have something like this
SELECT *
FROM Booking,Questionaires,Customers
WHERE accepted = 0
JOIN ON Booking_idBooking = Customers_idCustomers
Can anyone help me out?
Thanks beforehand!
You could use inner join this way
SELECT * FROM Booking_idBooking as BB
INNER JOIN Booking as B on BB.Booking_idBooking = B.idBooking
INNER JOIN Customers as C on BB.Customers_idCustomers = C.idCustomers
INNER JOIN Questionaires as Q on Q.idQuestionaires = B.uestionaires_idQuestionaires
WHERE B.accepted = 0 ;
I would go about doing something like this, but you may need left joins depending one your circumstances.
select
*
from
customers c
inner join booking_has_customers bhs on
bhs.Customers_idCustomers = c.idCustomers
inner join booking b on
b.idBooking = booking_idBooking
inner join Questionaires q on
q.idQuestionaires = b.Questionairs_idQuestionairs
where
b.accepted = 0;
Just a note, you should really change your naming conventions.
For example booking is singular, yet customers is plural in your schema. They should be consistent. Also, something like idBooking and booking_idBooking is not great - maybe use id and booking_id. Anyway just suggestions.
You also might want to invest some time in learning how to write joins. Checkout w3schools for good examples.
SELECT b.*,q.*,c.*
FROM Booking AS b
JOIN Questionaries AS q ON q.idQuestionaries=b.Questionaries_idQuestionaries
JOIN Booking_has_Customers AS bhc ON bhc.Booking_idBooking=b.idBooking
JOIN Customers AS c ON c.idCustomers=bhs.Customers_idCustomers
WHERE b.accepted=0
But come on, change ID names into simple id and you'll have:
SELECT b.*,q.*,c.*
FROM Booking AS b
JOIN Questionaries AS q ON q.id=b.id
JOIN Booking_has_Customers AS bhc ON bhc.id=b.id
JOIN Customers AS c ON c.id=bhs.id
WHERE b.accepted=0

SQL Join Issues Microsoft Query

I've been trying to write some code in SQL, but it keeps coming up with a syntax error regarding the join, and I can't work out why.
SELECT `COUNTRY$`.country_name, `PARTNER$`.partner_name, count(member_id)
FROM `Member$`
Left Join `COUNTRY$`
ON `MEMBER$`.country_id=`COUNTRY$`.country_id
lEFT jOIN `PARTNER$`
on `MEMBER$`.partner_ID = `PARTNER$`.partner_ID
Group By country_name,Partner_name
Any help would be appreciated.
May have something to do with how your table names are in 'thisFormat$'. Also you did not specify which table member_id was coming and group by also doesn't specify which table country_name, partner_name was from.
Try putting aliases on the tablenames and see if that eliminates the problem
SELECT c.country_name, p.partner_name, count(m.member_id)
FROM `Member$` m
left join `COUNTRY$` c on c.country_id = m.country_id
left join `PARTNER$` p on p.partner_id = m.partner_id
GROUP BY c.country_name, p.partner_name

Difference in MySQL JOIN vs LEFT JOIN

I have this cross-database query...
SELECT
`DM_Server`.`Jobs`.*,
`DM_Server`.servers.Description AS server,
digital_inventory.params,
products.products_id,
products.products_pdfupload,
customers.customers_firstname,
customers.customers_lastname
FROM `DM_Server`.`Jobs`
INNER JOIN `DM_Server`.servers ON servers.ServerID = Jobs.Jobs_ServerID
JOIN `cpod_live`.`digital_inventory` ON digital_inventory.jobname = Jobs.Jobs_Name
JOIN `cpod_live`.`products` ON products.products_pdfupload = CONCAT(digital_inventory.jobname, ".pdf")
JOIN `cpod_live`.`customers` ON customers.customers_id = products.cID
ORDER BY `DM_Server`.`Jobs`.Jobs_StartTime DESC LIMIT 50
it runs fine until I make them LEFT JOINs. I thought that by not specifying a type of join it was assumed to be a LEFT JOIN. Is this not the case?
I thought that by not specifying a type of join it was assumed to be a LEFT JOIN. Is this not the case?
No, the default join is an INNER JOIN.
Here is a visual explanation of SQL joins.
Inner join
Left join
No. When a type isn't specified, an INNER JOIN is used. To read up on differences; wikipedia
I believe the default is INNER JOIN if you just specify JOIN.
If you just mentioned JOIN in query by default it will be considered
as a INNER JOIN.
Left join:Left join will take all the elements from Left table and only matching records from the Right table as Follows.
example:
SELECT column_name(s)
FROM table_name1 #(Left table)
LEFT JOIN table_name2 #(Right table)
ON table_name1.column_name=table_name2.column_name
Hope this helps.