Get data from two tables - php

I have a query regarding join . Basically there are two tables product and product_boost . The table product_boost has the product_id as foreign key which is also in product table .
I want to get the data using join which is available in both the tables, and if not only data from first table will come.
I am using right outer join, here is my query
SELECT * FROM `vefinder_product`
RIGHT OUTER JOIN `vefinder_product_boost` ON `vefinder_product_boost`.`product_id`=`vefinder_product`.`product_id`
WHERE `vefinder_product`.`status` = 1
AND `vefinder_product`.`post_type` != 5
AND `vefinder_product`.`country` IN('348')
AND `vefinder_product`.`product_stock` >0
AND `vefinder_product`.`product_in_stock` = 1
AND `vefinder_product_boost`.`target_age_from` >= 20
AND `vefinder_product_boost`.`target_age_to` <= 40
ORDER BY `vefinder_product`.`is_boosted` DESC,
`vefinder_product`.`is_sponsered` DESC,
`vefinder_product`.`created_date` DESC LIMIT 21
How can i achive the desired thing , because this is not working. I am using codeigniter php.

Use Left join instead, if you want to get all the data from first (leftmost) table.
Any Where conditions on tables other than the first table (leftmost), should be shifted to ON condition in Left Join. Otherwise, Where would filter out unmatched rows also (null in the right side tables).
Try the following instead:
SELECT *
FROM `vefinder_product`
LEFT OUTER JOIN `vefinder_product_boost`
ON `vefinder_product_boost`.`product_id`=`vefinder_product`.`product_id` AND
`vefinder_product_boost`.`target_age_from` >= 20 AND
`vefinder_product_boost`.`target_age_to` <= 40
WHERE `vefinder_product`.`status` = 1 AND
`vefinder_product`.`post_type` != 5 AND
`vefinder_product`.`country` IN('348') AND
`vefinder_product`.`product_stock` >0 AND
`vefinder_product`.`product_in_stock` = 1
ORDER BY `vefinder_product`.`is_boosted` DESC,
`vefinder_product`.`is_sponsered` DESC,
`vefinder_product`.`created_date` DESC
LIMIT 21

Use left join and put where condition in ON cluase
SELECT * FROM `vefinder_product`
left OUTER JOIN `vefinder_product_boost` ON `vefinder_product_boost`.`product_id`=`vefinder_product`.`product_id`
and `vefinder_product`.`status` = 1
AND `vefinder_product`.`post_type` != 5
AND `vefinder_product`.`country` IN('348')
AND `vefinder_product`.`product_stock` >0
AND `vefinder_product`.`product_in_stock` = 1
AND `vefinder_product_boost`.`target_age_from` >= 20
AND `vefinder_product_boost`.`target_age_to` <= 40
ORDER BY `vefinder_product`.`is_boosted` DESC,
`vefinder_product`.`is_sponsered` DESC,
`vefinder_product`.`created_date` DESC LIMIT 21

you can use third party software like SQLyog.
it is very simple for join query just build query with UI and assign relation to that fields between tables.
in sqlyog you can get data from multiple tables not only two tables.
because i am currently using this software for time saving.

Related

Solution for "Subquery returns more than 1 row"

I'm trying to substitute my join SQL code to a different code without any of JOIN statements for faster data retrieval. However, i'm getting the error below.
#1242 - Subquery returns more than 1 row
What i would like to do, get all rows from one table 'tbl_my_itemlist' and JOIN to other more tables, tbl_register and tbl_register without using JOIN statements.
The Code using JOIN statement (works fine).
SELECT
tbl_screenshots.screenshot_image_url,
mit.my_itemlist_id,
mit.item_name,
mit.item_initial_cost,
mit.item_offer_cost,
mit.offer_date_from,
mit.offer_date_to
FROM
(
SELECT
my_itemlist_id
FROM
tbl_my_itemlist
WHERE
offer_date_from >='2020-10-20' AND offer_date_to <= '2020-10-30' AND
item_deleted_status = 'active'
) mlist
JOIN tbl_my_itemlist mit ON
mit.my_itemlist_id = mlist.my_itemlist_id
RIGHT JOIN tbl_screenshots ON mit.my_itemlist_id =
tbl_screenshots.my_itemlist_id
RIGHT JOIN tbl_register ON tbl_register.register_id = mit.register_id
GROUP BY
mit.my_itemlist_id
ORDER BY mit.offer_date_to ASC LIMIT 2
The code i'm substituting the JOIN statement code with.
SELECT
mit.my_itemlist_id,
mit.item_name,
mit.item_initial_cost,
mit.item_offer_cost,
mit.offer_date_from,
mit.offer_date_to,
(
SELECT
reg.business_name
FROM
tbl_register reg
WHERE
reg.register_id = mit.register_id
) reg_sql,
(
SELECT
sshots.screenshot_image_url
FROM
tbl_screenshots sshots
WHERE
sshots.my_itemlist_id = mit.my_itemlist_id
) sshots_sq
FROM
tbl_my_itemlist mit
WHERE
mit.offer_date_from >= '2020-10-20' AND mit.offer_date_to <= '2020-10-30' AND mit.item_deleted_status = 'active'
GROUP BY
mit.my_itemlist_id
ORDER BY
mit.offer_date_to ASC
LIMIT 2
I'm trying to build an SQL query that can retrieve data from million records within very short period of time as compared to using the JOIN statement.

How select next row pagination in sql

I'm sorry I'm weak for English.
i echo 2 row in each page . how echo next 2 row
SELECT *
FROM `mzmx_post`
JOIN mzmx_post_category
WHERE mzmx_post.id = mzmx_post_category.post_id AND zmx_post_category.category_id = 5
ORDER BY id DESC
LIMIT 2
You can use the two-arguments form of LIMIT to offset the result by a given number of rows, like:
SELECT *
FROM `mzmx_post`
JOIN mzmx_post_category ON mzmx_post.id = mzmx_post_category.post_id
WHERE mzmx_post_category.category_id = 5
ORDER BY id DESC
LIMIT 2, 2 -- fetch records 3 and 4
This gives you the second page. If you want the third page, then:
LIMIT 4, 2
And so on.
Note that I modified your query so the joining condition between the tables is placed in the ON clause of the join rather than in the WHERE clause.
Better add one extra column (e.g. mzmx_post_key bigint) of Long type in each table and have sequential value on that column. Use that column to fetch data from DB from page wise.
sqL suery should look like:
SELECT *
FROM `mzmx_post`
JOIN mzmx_post_category ON mzmx_post.id = mzmx_post_category.post_id
WHERE mzmx_post_category.category_id = 5 and mzmx_post_key> ##last record key##
ORDER BY mzmx_post_key ASC
LIMIT 2
The basic idea is to use
LIMIT n,o
where n is the results per page
o is the offset from the first result
for the p-th page the offset would be
o = p * n
where p = 0,1,2,....

MySQL #1241 - Operand should contain 1 column(s) on counting

I am running this query, and I am getting ** #1241 - Operand should contain 1 column(s)** error:
SELECT `forumCategories`.`id`, `forumCategories`.`name`, `forumCategories`.`order`, `forumCategories`.`description`, `forumCategories`.`date_created`, COUNT(forumPosts.forumCategory_id) as postCount,
(SELECT `forumPosts`.*, `forumChildPosts`.`id`, `forumChildPosts`.`forumPost_id`, COUNT(forumChildPosts.forumPost_id) as childCount FROM `forumChildPosts` LEFT JOIN `forumPosts` ON `forumPosts`.`id` = `forumChildPosts`.`forumPost_id` GROUP BY `forumPosts`.`id`) AS childCount
FROM `forumCategories`
LEFT JOIN `forumPosts` ON `forumCategories`.`id` = `forumPosts`.`forumCategory_id`
GROUP BY `forumCategories`.`id`
ORDER BY `forumCategories`.`order` DESC
I have 3 tables:
forumCategories
forumPosts | forumPosts.forumCategory_id = forumCategories.id
forumChildPosts | forumChildPosts.forumPosts_id = forumPosts.id
I want to count all posts for the forum category, and them I want to count all the child posts that belongs to that forum category. How can I do this?
You can't select several items with a subselect and then give them one name. Now you're getting everything from forumPosts, something from forumChildPosts etc and trying to put that into a single column, childCount. This is not allowed.
It might be enough to remove all other result columns from that select and only leave the count?
I couldn't try it, is that makes sense ? But you can't get nested results from mysql due to its limitation, MYSQL is a Matrix table.
SELECT `forumCategories`.`id`,
`forumCategories`.`name`,
`forumCategories`.`order`,
`forumCategories`.`description`,
`forumCategories`.`date_created`,
COUNT(forumPosts.forumCategory_id) AS postCount,
(SELECT COUNT(forumChildPosts.forumPost_id) AS childCount FROM `forumChildPosts` LEFT JOIN `forumPosts` ON `forumPosts`.`id` = `forumChildPosts`.`forumPost_id` GROUP BY `forumPosts`.`id`) AS childCount
FROM `forumCategories`
LEFT JOIN `forumPosts` ON `forumCategories`.`id` = `forumPosts`.`forumCategory_id`
GROUP BY `forumCategories`.`id`
ORDER BY `forumCategories`.`order` DESC

sql join not displaying correctly

I'm currently trying to join two tables with a left join:
--portal--
id_portal (index)
id_venue
name_portal
--access--
id_access (index)
id_event
id_portal
id_tickets
scan_access
'access' contains a number of ticket types per portal for each event. I need to combine these to get the sum total of the scan_access column for each portal but include the portals that have 'null' scan_access to come up with '0'. To achieve this I've used a left join:
SELECT portal.name_portal, SUM(access.scan_access) AS total_scan
FROM portal LEFT JOIN access ON portal.id_portal = access.id_portal
WHERE portal.id_venue = $venueId
GROUP BY portal.id_portal
ORDER BY portal.id_portal ASC
which means I get the following:
Portal 1 - Null
Portal 2 - 40
Portal 3 - 33
Portal 4 - Null
but I have an issue when I need to also get the above result when taking into account the event (id_event) because when I use the following:
SELECT portal.name_portal, SUM(access.scan_access) AS total_scan
FROM portal LEFT JOIN access ON portal.id_portal = access.id_portal
WHERE portal.id_venue = $venueId AND access.id_event = 20
GROUP BY portal.id_portal
ORDER BY portal.id_portal ASC
I get:
Portal 2 - 40
Portal 3 - 33
which makes sense as those are the only two rows that have an id_event value. But how can I take this col into account without losing the other portals? also, is there a way in sql to make the 'null' a zero when returning a result? (I can fix the null after with php but wanted to see if it was possible)
By putting access.id_event = 20 in your WHERE clause, you turn your LEFT JOIN into an INNER JOIN. Move access.id_event = 20 into your join criteria to preserve your LEFT JOIN. As #echo_me mentioned, you can use COALESCE() to get rid of your zeroes. I'd put it around the SUM(), instead of inside.
SELECT portal.name_portal, COALESCE( SUM(access.scan_access), 0 ) AS total_scan
FROM portal LEFT JOIN access ON portal.id_portal = access.id_portal AND access.id_event = 20
WHERE portal.id_venue = $venueId
GROUP BY portal.id_portal
ORDER BY portal.id_portal ASC
to convert NULL to 0 use this
COALESCE(col, 0)
in your example it will be
SUM(COALESCE(access.scan_access, 0)) AS total_scan

How to merge two MySQL queries together

How can I merge the following queries together?
To get all the objects of a particular type I use
SELECT ID FROM social_objects
WHERE subgroup='23' ORDER BY time_created DESC LIMIT 0 , 30
I have this search too, for titles
SELECT ID FROM 'social_objects_single'
WHERE 'title' LIKE '%indian%' LIMIT 0 , 30
How can I get only objects of subgroup 23 with certain titles?
How are the two tables related? If they both reference an ID you inner join and use AND to combine conditions:
SELECT Parent.ID, Child.ID
FROM ParentTable
INNER JOIN ChildTable ON ParentTable.ID = ChildTable.ForeignKeyID
WHERE Parent.ID = 23 AND Title LIKE '%indian%'
If your social_objects_single has the same ID as the social_objects table you could do this:
SELECT so.ID FROM social_objects so
INNER JOIN social_objects_single soi ON soi.ID = so.ID
WHERE so.subgroup = 23 AND soi.title LIKE '%indian%'
ORDER BY so.time_created DESC LIMIT 0, 30;
SELECT SO.ID,SOS.ID
FROM social_objects SO ,social_objects_single SOS WHERE SOS.title LIKE '%indian% and SO.subgroup=23 and SOS.id =SO.subgroup_id
you should replace the last condition SOS.id =SO.subgroup_id as your tables are connected

Categories