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
Related
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.
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......
I try to write a query in order to retrieve full info for our product in virtuemart 2.
First I try with single INNER JOIN like the example bellow:
$query ="select p.`virtuemart_product_id`, p.`product_name`, p.`product_s_desc`, `pmqbw_virtuemart_product_categories`.`virtuemart_category_id`
FROM `pmqbw_virtuemart_products_el_gr` AS p
INNER JOIN `pmqbw_virtuemart_product_categories` ON p.`virtuemart_product_id`=`pmqbw_virtuemart_product_categories`.`virtuemart_product_id`
LIMIT 0 ,10";
The above worked well!
Then I tried to be more complex and I wrote the following:
$query ="select p.`virtuemart_product_id`, p.`product_name`, p.`product_s_desc`, `pmqbw_virtuemart_product_categories`.`virtuemart_category_id`, `pmqbw_virtuemart_categories_el_gr`.`category_name`
FROM `pmqbw_virtuemart_products_el_gr` AS p
INNER JOIN p
ON `pmqbw_virtuemart_product_categories`.`virtuemart_product_id`=p.`virtuemart_product_id`
INNER JOIN `pmqbw_virtuemart_categories_el_gr` AS x
ON x.`virtuemart_category_id`=`pmqbw_virtuemart_product_categories`.`virtuemart_category_id`
LIMIT 0 ,10";
But something goes wrong here and I cannot understand.
After that I'll try to retrieve more tables and more joins in order to bring product data I need.
Thank you for your help in advance!
Actually I want to retrieve data from the following tables.
From pmqbw_virtuemart_products_el_gr I want fields: product_name, product_s_desc,virtuemart_product_id`
From pmqbw_virtuemart_product_categories I want fields: virtuemart_product_id, virtuemart_category_id
From pmqbw_virtuemart_categories_el_gr I want fields: virtuemart_category_id, category_name
From pmqbw_virtuemart_product_medias I want fields: virtuemart_product_id, virtuemart_media_id
pmqbw_virtuemart_medias I want fields: virtuemart_media_id, file_url, file_url_thumb where file_mimetype=image/jpeg AND file_type=product
I tried a different query with left joins but does not work as well.
$sql = "SELECT N.product_name,I.virtuemart_media_id,M.virtuemart_product_id,MN.category_name
FROM pmqbw_virtuemart_categories_el_gr AS M
LEFT JOIN pmqbw_virtuemart_products AS P ON P.virtuemart_product_id = M.virtuemart_product_id
LEFT JOIN pmqbw_virtuemart_products_el_gr AS N ON N.virtuemart_product_id = M.virtuemart_product_id
LEFT JOIN pmqbw_virtuemart_product_medias AS I ON I.virtuemart_product_id = M.virtuemart_product_id
LEFT JOIN pmqbw_virtuemart_product_categories AS MN ON MN.virtuemart_category_id = M.virtuemart_category_id
WHERE M.virtuemart_category_id = 1";
Any help will be appreciated!
Thank you!
Thank you all for the help!
the query that runs ok is the following:
$sql = "SELECT DISTINCT X.virtuemart_product_id, X.product_name, M.category_name, Y.virtuemart_product_id, W.file_url
FROM pmqbw_virtuemart_products_el_gr AS X
INNER JOIN pmqbw_virtuemart_product_categories AS P ON P.virtuemart_product_id = X.virtuemart_product_id
AND P.virtuemart_category_id =2
INNER JOIN pmqbw_virtuemart_categories_el_gr AS M ON M.virtuemart_category_id = P.virtuemart_category_id"
INNER JOIN pmqbw_virtuemart_product_medias AS Y ON Y.virtuemart_product_id = X.virtuemart_product_id
INNER JOIN pmqbw_virtuemart_medias AS W ON W.virtuemart_media_id = Y.virtuemart_media_id;
The bud thing is that every product has two or more images and I only want one.
I have to think something in order to eliminate it!
If anyone has something in his mind for help will be more than welcome!
Thank you!
Ok, I find a solution with GROUP BY Y.virtuemart_product_id
:)
Thank you all !
My query is as follows:
SELECT collection_content_mappings.tbl_content_common_id
FROM collection_content_mappings Inner Join tbl_content_commons ON collection_content_mappings.tbl_content_common_id = tbl_content_commons.id
INNER JOIN tbl_content_additionals ON collection_content_mappings.tbl_content_common_id = tbl_content_additionals.content_content_code
WHERE collection_content_mappings.collection_id = 1
This part is the problem:
collection_content_mappings.tbl_content_common_id = tbl_content_additionals.content_content_code
The field "collection_content_mappings.tbl_content_common_id" is not the one that need to be equated there; instead I have to take tbl_content_commons.content_common_code from tbl_content_commons where collection_content_mappings.tbl_content_common_id = tbl_content_commons.id . How do I add this to the query above?
Any help is appreciated
You should really rename your tables when you are building the query.
I don't know if I understand properly what you want, but I think that is something like this:
SELECT tbl_content_commons.id
FROM tbl_content_commons
Inner Join collection_content_mappings ON collection_content_mappings.tbl_content_common_id = tbl_content_commons.id
INNER JOIN tbl_content_additionals ON tbl_content_additionals.content_content_code = tbl_content_commons.content_common_code
WHERE tbl_content_commons.id = 1
I have the following model:
I've been at this for a while and still don't know how to tackle it properly. Already looked at joining two aliased subqueries, joining two views, and tried a gynormous and ugly all-in one query, none of which worked.
My question is simple:
How can I select deivce_names.name and match them to a model and manufacturer?
SELECT
name, manufacturer, model
FROM
device_names
JOIN devices ON (device_names.id = name_id)
JOIN devices_generic ON (generic_device_id = devices_generic.id)
JOIN device_manufacturers ON (manufacturer_id = device_manufacturers.id)
JOIN device_models ON (model_id = device_models.id)
select
device_names.name as device_name,
device_manufacturers.name as device_manufacturer_name,
device_models.name as device_model_name
from deivce_names
join device on device.name_id = deivce_names.id
join devices_generic on devices_generic.id = devices.generic_device_id
join device_manufacturers on devices_generic.manufacturer_id = device_manufacturers.id
join device_models on device_models.id = devices_generic.model_id
SELECT dm1.manufacturer, dm2.model from device_manufacturers as dm1 join devices_generic as dg on dg.manufacturer_id = dm1.id join device_models as dm2 on dm2.id = dg.model_id join devices as d2 on d2.generic_device_id = dg.id join device_names as dn on dn.id = d2.name_id where name = 'foo'
In the future, you might want to simply put your data into a fiddle and play with it there.