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
Related
I'm trying to join four tables together. The code works fine when I'm only comparing the EVENT_DATA and joining the PERSONA tables as I'm able to get the "Name" column from PERSONA (which doesn't exist in the EVENT_DATA table). However, one of the problems is that this "Name" column also exists in the CUSTOMCAR table, so I can only get one or the other. Additionally, when I tried adding the last join statement, the code wouldn't run at all.
If someone could please help me, that would be great!
$sql = "SELECT * FROM EVENT_DATA
LEFT JOIN PERSONA ON EVENT_DATA.personaId = PERSONA.ID
LEFT JOIN CUSTOMCAR ON CUSTOMCAR.ownedCarId = EVENT_DATA.carId
LEFT JOIN CARCLASSES ON CARCLASSES.store_name = CUSTOMCAR.name
WHERE (EVENT_DATA.EVENTID = '299')";
You should avoid * and use explicit columns list instead:
SELECT EVENT_DATA.personaId, ...
FROM EVENT_DATA
LEFT JOIN PERSONA ON EVENT_DATA.personaId = PERSONA.ID
LEFT JOIN CUSTOMCAR ON CUSTOMCAR.ownedCarId = EVENT_DATA.carId
LEFT JOIN CARCLASSES ON CARCLASSES.store_name = CUSTOMCAR.name
WHERE (EVENT_DATA.EVENTID = '299');
If you have same column name you need to to use aliasing:
SELECT ...
CUSTOMCAR.NAME AS c_name,
PERSONA.NAME AS p_name
...
You could also use Aliasing:
$sql = "SELECT ed.*,
pa.*,
cc.*,
ccs.*
FROM EVENT_DATA AS ed
LEFT JOIN PERSONA AS pa ON ed.personaId = pa.ID
LEFT JOIN CUSTOMCAR AS cc ON cc.ownedCarId = e.carId
LEFT JOIN CARCLASSES AS ccs ON ccs.store_name = cc.name
WHERE (ed.EVENTID = '299')";
Note: Although as suggested by #Lukasz, you should really avoid using wildcard (*), and provide an explicit list of columns in the SELECT clause.
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
I want to join on the same table twice to get username and username_to
#array = UsersWalletsBalancesFrozen.
joins("INNER JOIN userds userdsidto ON transactions.user_id_to=userdsidto.id").
joins("INNER JOIN userds userdsid ON transactions.user_id = userdsid.id").select("*")
This get me the last join only works i think it's overrides is there any solution to that
Just as a quick solution... Have you tried to put it in the same method call:
#array = UsersWalletsBalancesFrozen.joins("INNER JOIN userds AS user_to userdsidto ON transactions.user_id_to=user_to.id INNER JOIN userds userdsid AS user_from ON transactions.user_id = user_from.id").select("transactions.*, user_to.username, user_from.username")
?
PS: select('*') will be called by default. No need to explicitly specify it.
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 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 !