SQL Query into Laravel Query Builder - mysql

I have this SQL Query and at the moment I don't know how to build this with the larval query builder
SELECT h1.Name, h2.Torvorlage from
(SELECT Name, Player_ID FROM `Spieler` WHERE Vereins_ID = 1) h1
left JOIN
(SELECT Torvorlage, Player_ID FROM `SpielerStats` left join Spielplan on (SpielerStats.Spielplan_ID = Spielplan.Spielplan_ID) WHERE Spielplan.Spielplan_ID = 1 and Spielplan.Spieltag = 2) h2
on h1.Player_ID = h2.Player_ID
Can anyone give me a example how to build this?

Try this.
DB::table('Spieler')->where('Vereins_ID','=',1)
->join('SpielerStats','SpielerStats.Player_ID','=','Spieler.Player_ID','left')
->select('SpielerStats.Torvorlage','Spieler.Name')
->join('Spielplan','SpielerStats.Spielplan_ID','=','Spielplan.Spielplan_ID','left')
->where('Spielplan.Spielplan_ID','=',1)
->where('Spielplan.Spieltag','=',2)->get();
Or
$d = DB::raw('(SELECT Torvorlage, Player_ID FROM `SpielerStats` left join Spielplan on (SpielerStats.Spielplan_ID = Spielplan.Spielplan_ID) WHERE Spielplan.Spielplan_ID = 1 and Spielplan.Spieltag = 2) as h2')
DB::table('Spieler')
->select('Spieler.Name','h2.Torvorlage')
->leftJoin($d, 'h2.Player_ID', '=', 'Spieler.Player_ID')
->where('Vereins_ID','=',1)
->get();

In order to get a better answer, you should explain what your query is doing. What is the relationship with these tables? In my experience, when I had a complicated SQL query to convert to Eloquent, I found that defining the relationships on the models solved the problem.

I solved this with DB:Select
$spielerstats = DB::Select('SELECT h1.Name, h2.Torvorlage from
(SELECT Name, Player_ID FROM `Spieler` WHERE Vereins_ID = 1) h1
left JOIN
(SELECT Torvorlage, Player_ID FROM `SpielerStats` left join Spielplan on (SpielerStats.Spielplan_ID = Spielplan.Spielplan_ID) WHERE Spielplan.Spielplan_ID = 1 and Spielplan.Spieltag = 1) h2
on h1.Player_ID = h2.Player_ID');
I'm not sure if this is the best way but it works.

Related

How to join many tables without duplicate result?

Hello all StackOverFlow families.
I need your help about sql query in mysql. I join four tables but result is duplicate row.
I have tried by using GROUP BY but not work.
Here is my query:
SELECT `tbl_leave`.`id`, `tbl_leave`.`staff_id`, `tbl_leave`.`type_id`, `tbl_leave`.`start_date`, `tbl_leave`.`end_date`,
`tbl_leave`.`total_days`, `tbl_leave`.`reason`, `tbl_leave_type`.`type`, `tbl_employment`.`com_id` as `comid`, `tbl_staff`.`name`
FROM `tbl_leave` JOIN
`tbl_leave_type`
ON `tbl_leave_type`.`id` = `tbl_leave`.`type_id` JOIN
`tbl_employment`
ON `tbl_employment`.`staff_id` = `tbl_leave`.`staff_id` JOIN
`tbl_staff`
ON `tbl_staff`.`id` = `tbl_leave`.`staff_id`
You can look as picture: https://imgur.com/gallery/1Ewku
And this is relationship table: https://imgur.com/gallery/ziKq3
The result I want like this : https://imgur.com/gallery/6NJpR
Thank for your valuable times for this question.
Please try
SELECT distinct `tbl_leave`.`id`, `tbl_leave`.`staff_id`,
`tbl_leave`.`type_id`, `tbl_leave`.`start_date`, `tbl_leave`.`end_date`,
`tbl_leave`.`total_days`, `tbl_leave`.`reason`, `tbl_leave_type`.`type`,
`tbl_employment`.`com_id` as `comid`, `tbl_staff`.`name`
FROM `tbl_leave` JOIN
`tbl_leave_type`
ON `tbl_leave_type`.`id` = `tbl_leave`.`type_id` JOIN
`tbl_employment`
ON `tbl_employment`.`staff_id` = `tbl_leave`.`staff_id` JOIN
`tbl_staff`
ON `tbl_staff`.`id` = `tbl_leave`.`staff_id`;
Please try this solution and let me know if is there any problem occur:
SELECT [Columns] From tbl_staff as staff
JOIN tbl_employment as emp on staff.id = = emp.staff_id
JOIN tbl_leave as leave on staff.id = = leave.staff_id
JOIN tbl_leave_type as ltype on leave.type_id = = ltype.id

Mysql row query to codeigniter query

I have the following Mysql query. Running as needed.
SELECT
t_doc.*, t_user.IDA, t_data.IDA
FROM
( SELECT DISTINCT IDau FROM t_doc ) IDau_list
LEFT OUTER JOIN
(
SELECT
IDd ,IDau
FROM
t_doc
WHERE
doc_type = 'doc'
GROUP BY IDau
)
doc_images ON doc_images.IDau = IDau_list.IDau
LEFT OUTER JOIN
(
SELECT
IDd ,IDau
FROM
t_auto_doc
WHERE
doc_type = 'jpg'
GROUP BY IDau
)
jpg_images ON jpg_images.IDau = IDau_list.IDau
LEFT OUTER JOIN
t_doc ON t_doc.IDdoc = COALESCE(jpg_images.IDd, doc_images.IDd)
LEFT OUTER JOIN
t_user ON t_user.IDau = t_doc.IDau
LEFT OUTER JOIN
t_data ON t_user.IDA = t_data.IDA
But, What I must bring this query to codeigniter Model, in which I have to adapt the query, as example like this...
$this->db->select('u.IDau, a.*');
$this->db->from('t_user u');
$this->db->join('t_doc d', 'd.IDau=u.IDau', 'left');
And ...More.....Here......
But, I got it difficult. Is some one out there might change it Please..
Thanks in advance
As #NEVERMIND Suggest I have accomplish the task using...
$query = $this->db->query("My QUERY WITH A BIT OF CHANGE");
Yes, I should make some change to get the exact Data as i needed.
Thanks for all of your comments and ideas.

Two related queries needs to be one

I know this is a basic thing but SQL is a serious weak point of mine...
I have the following query ("query1")
select
SC.statues, SC_some_real_code
from
[corpdb_gs].[dbo].[Simple_Codes] SC
inner join
[corpdb_gs].[dbo].[real_simple_essentials] RSE
on
SC.statues = RSE.se_statutes
AND
SC.some_real_code = RSE.se_statutes_reason
I need to return only the rows in table [db2].[dbo].[statusYo] where
statusYo.code = "query1".SC.statues
AND
statusYo.reason = "query1".SC.some_real_code
Help?
select * from [db2].[dbo].[statusYo] query2
inner join
(select
SC.statues as statues , SC.some_real_code as some_real_code
from
[corpdb_gs].[dbo].[Simple_Codes] SC
inner join
[corpdb_gs].[dbo].[real_simple_essentials] RSE
on
SC.statues = RSE.se_statutes
AND
SC.some_real_code = RSE.se_statutes_reason) query1
on query1.statues =query2.code
and query1.some_real_code=query2.reason
this will work for you......

How do I create the SQL for this relationship?

How do I get the survey sections by product id using the best method?
Also if this relationship has a name please provide it in your answer.
As requested my attempt:
SELECT *
FROM survey_sections
JOIN survey_sections_map ON survey_sections_map.survey_section_id = survey_sections.id
JOIN product_tags_map
WHERE product_tags_map.product_id = 2
second attempt...
SELECT *
FROM survey_sections
JOIN survey_sections_map ON survey_sections_map.survey_section_id = survey_sections.id
JOIN product_tags_map ON product_tags_map.product_tag_id = survey_sections_map.product_tag_id
WHERE product_tags_map.product_id = 2
I think We can use nested select queries as well, don't know this is the best way or not but you should try it ones.
SELECT sur.name
FROM survey_sections AS sur
INNER JOIN survey_sections_map AS surmap ON sur.id = surmap.survey_section_id
WHERE surmap.product_tag_id IN (SELECT product_tag_id FROM product_tags_map WHERE product_id = 2)

MySQL statement, JOIN

I have these tables :
I don't know how I can write a statement, that takes emails from Table "Firm", that have Location_id = '1' and Category_id = '130';
I know that I should use JOINs, but I'm not sure how to go from there.
SELECT Firm.email
FROM Firm
INNER JOIN FirmID ON Firm.firma_id = FirmID.firma_id
WHERE FirmID.location_id = '1'
AND FirmID.Category_id = '130'
Should be as simple as doing the following:
SELECT email
FROM Firm, FirmID
WHERE Firm.firma_id = FirmID.firma_id
AND FirmID.location_id = 1
AND FirmID.category_id = 130;
It does a join behind the scenes, but can be a bit clearer to understand than using the JOIN keyword.
You could do:
SELECT f.email
FROM Firm f
WHERE f.firma_id =
(
SELECT ff.firma_id
FROM FirmID ff
WHERE ff.location_id = 1
AND ff.category_id = 130
)
Using an inner select.
But using JOINS is in the long term the way to go, what have you tried and what's not working?