invalid records using inner join from two tables - php

I have this sql statement generated from codeigniter
SELECT * FROM (`products`) LEFT JOIN `products_attributes` ON `products_attributes`.`product_id` = `products`.`id` WHERE `products`.`category` = '1' AND `products`.`sub_category` = '1' AND `products_attributes`.`attribute_id` = '3' AND `products_attributes`.`attribute_id` = '4' AND `products_attributes`.`attribute_id` = '5' GROUP BY `products`.`id`
and I have these tables
products :
products_attributes :
I need to get the products with the all attributes only, so I will get the product id = 1 if the attributes are 3,4,5 and if the attributes are 3,4,5,6 I will not get product id = 1

Your sql query is totally wrong. What you did is:
Give me the record the attribute_id = 3 AND attribute_id = 4 AND attribute_id = 5 AND attribute_id = 6. One row can not be equal to 3 and 4 at the same time.
What you need can be achieved in many different ways. For example:
SELECT
products.id
FROM products
JOIN products_attributes ON products_attributes.product_id = products.id
WHERE
products.category = '1'
AND `products_attributes`.`attribute_id` IN (3,4,5,6)
GROUP BY
products.id
HAVING COUNT(DISTINCT `products_attributes`.`attribute_id`) = 4
If you specify just IN ... then it will take all the records who has at least 1 such attribute_id

Try this:
SELECT * FROM (`products`)
LEFT JOIN `products_attributes`
ON `products_attributes`.`product_id` = `products`.`id`
WHERE `products`.`category` = 1
AND `products`.`sub_category` = 1
AND `products_attributes`.`attribute_id` IN (3, 4, 5)
GROUP BY `products`.`id`

Related

How to select few values with and condition?

I have table example http://prntscr.com/msqpep
I need empty result when i am doing
SELECT * FROM prods
WHERE prod_id = 3 and (fet_id = 1 and fet_id = 5)
but that code so not work (fet_id = 1 and fet_id = 5) or (fet_id = 1 and fet_id = 3)
"Where in" is a best way but i need `WHERE IN(1 and 2 and 3)
By default "WHERE IN" working as (1 or 2 or 3)
When I select fet_id=1 and fet_id=3 i need two results
When i select fet_id=1 and fet_id=5 i need no result
You probably want
SELECT prod_id FROM prods GROUP by prod_id
HAVING (SUM(fet_id=1)>0
AND SUM(fet_id=5)>0)
OR
(SUM(fet_id=1)>0
AND SUM(fet_id=3)>0)
HAVING filters groups WHERE filters rows.
Try This:
SELECT p1.id AS id1, p2.id AS id2
FROM `prods` p1 LEFT JOIN `prods` p2 ON p1.`prod_id` = p2.`prod_id`
WHERE p1.`fet_id` = '1' AND p2.`fet_id` = '3'
Output
id1 id2
1 3
With input fet_id=1 & fet_id=5
SELECT p1.id AS id1, p2.id AS id2
FROM `prods` p1 LEFT JOIN `prods` p2 ON p1.`prod_id` = p2.`prod_id`
WHERE p1.`fet_id` = '1' AND p2.`fet_id` = '5'
Output: No result
And another one if need more then two values
SELECT res.* FROM (SELECT 3 as total,
(SELECT COUNT(*) FROM `prods` as t1 WHERE t1.`fet_id` = 1 OR t1.`fet_id` = 2 OR t1.`fet_id` = 5) as current,
t2.*
FROM `prods` as t2 WHERE t2.`fet_id` in (1,2,5)) as res WHERE res.total = res.current

Invalid use of group function - How i can use SUM function inside the SUM ? or is there any other way to get same results? Below is my query

SELECT product_date dt,account_id, account_name, account_accounttypeID,
SUM(SUM(invoicedetail_quantity * invoicedetail_price)) cr, 0 dr
FROM product
JOIN invoice_detail ON invoicedetail_itemID = product_id
JOIN invoice ON invoice_id = invoicedetail_invoiceID
JOIN account ON account_id = product_inventoryaccountID
WHERE invoice_companyID = '49'
AND invoicedetail_itemtype = 'PR'
AND invoice_invoicedate BETWEEN '2016-01-01' AND '2016-01-31'
AND invoice_status = 1

How to get single row from mysql databse table using inner join query?

I have following 2 tables in mysql database :
1) products
p_id p_title p_price u_id
----------------------------------
1 tile 123 25
2 tile 133 24
3 tile 143 25
2) product_images
id img_name p_id u_id
1 name 1 25
2 name 1 25
3 name 2 24
I want to get all row from products table and only one row from product_images table. So to get this I am using following sql query but it's return all rows from product_images table. Should be one !
My Query :
$get_menu = mysqli_query($conn,
"SELECT DISTINCT products.p_title, products.p_price,
product_images.p_list_image, chef_profile.live_at, chef_profile.fname,
chef_profile.lname FROM products LEFT JOIN product_images ON products.p_id
= product_images.p_id LEFT JOIN chef_profile ON products.u_id =
chef_profile.u_id GROUP BY product_images.p_list_image") or die('sorry');
I am using this query in php while loop so that I want to show all unique data from products table and only one image from product_images table.
$num_menu = mysqli_num_rows($get_menu);
while( $get_result = mysqli_fetch_array($get_menu) ) {
/*echo '<pre>';
print_r($get_result);*/
$menu_title = htmlspecialchars($get_result['p_title']);
$menu_price = htmlspecialchars($get_result['p_price']);
$menu_image = htmlspecialchars($get_result['p_list_image']) ? htmlspecialchars($get_result['p_list_image']) : "no-menu-preview.png";
$live_at = htmlspecialchars($get_result['live_at']);
$fname = htmlspecialchars($get_result['fname']);
$lname = htmlspecialchars($get_result['lname']);
// echo..........data.. her...
}
There you have it.
You only have to group by p_id to get 1 result for each product.
SELECT DISTINCT products.p_title, products.p_price, product_images.p_list_image, chef_profile.live_at, chef_profile.fname, chef_profile.lname
FROM products
LEFT JOIN product_images ON products.p_id = product_images.p_id
LEFT JOIN chef_profile ON products.u_id = chef_profile.u_id
GROUP BY products.p_id
Hope it helps ;)
SELECT products.p_title,
products.p_price,
product_images.p_list_image,
chef_profile.live_at,
chef_profile.fname,
chef_profile.lname
FROM products
LEFT JOIN product_images ON products.p_id = product_images.p_id
LEFT JOIN chef_profile ON products.u_id = chef_profile.u_id
GROUP BY products.p_id,product_images.p_list_image

Laravel 4 ManyToMany where

I have 3 tables:
products (->belongsToMany('Series'))
series (->belongsToMany('Product'))
product_series
If I want to take products with series = 3:
Series::find(3)->products;
Produced sql query:
SELECT *
FROM `products`
INNER JOIN `product_series`
ON `products`.`id` = `product_series`.`product_id`
WHERE `product_series`.`series_id` = '3'
The question is how can I take products where series_id != 3 with Eloquent?
The sql query is smth like:
SELECT *
FROM `products`
INNER JOIN `product_series`
ON `products`.`id` = `product_series`.`product_id`
WHERE `product_series`.`series_id` != '3'
You may try this:
$series = Series::where('id', '!=', 3)->with('products')->get();

MySQL: OUTER JOIN and COUNT()

I have table 'posters', table 'reviews' and table 'trailers'. Every table is connected with an movieID column, but each table can be empty for certain movieIDs:
++ posters_table +++ ++ reviews_table ++ ++ trailers_table ++
--itemID--+--filename- --itemID--+--review-- --itemID--+--trailer
---------------------- --------------------- ---------------------
----001---+--0012343-- ----004---+--blalba-- ----002---+--002345--
----001---+--0013331-- ----004---+--xlalxa-- ----005---+--005434--
----002---+--0020052-- ----005---+--zlalza-- ----001---+--005335--
I want to COUNT() the number of posters, reviews and trailers for the specified movieID and get 0 if no available.
So if I want to count movieID = 001 I get: ['posters'] = 2 / ['reviews'] = 0 and ['trailers'] = 1 (for example)
Can someone post the SQL query to do this?
select
(select count(*) from posters_table where itemId = ?) as posters,
(select count(*) from reviews_table where itemId = ?) as reviews,
(select count(*) from trailers_table where itemId = ?) as trailers;
I think if you JOIN the movie table to a result set that gives the count, you can then pick out whether or not the count is > 0 and give the appropriate value:
SELECT movieID, IF(posters.posters_count > 0, posters.posters_count,0) AS posters_total, IF(reviews.reviews_count > 0, reviews.reviews_count,0) AS reviews_total, IF(trailers.trailers_count > 0, trailers.trailers_count,0) AS trailers_total
FROM movies m
LEFT JOIN (SELECT itemID,COUNT(*) AS posters_count FROM posters_table WHERE itemID = '001' GROUP BY itemID) posters ON posters.itemID = movies.movieID
LEFT JOIN (SELECT itemID,COUNT(*) AS reviews_count FROM reviews_table WHERE itemID = '001' GROUP BY itemID) reviews ON reviews.itemID = movies.movieID
LEFT JOIN (SELECT itemID,COUNT(*) AS trailers_count FROM trailers_table WHERE itemID = '001' GROUP BY itemID) trailers ON trailers.itemID = movies.movieID
WHERE m.movieID = '001'
EDIT: I prefers ar's solution. Much simpler!
select
(select count(P.movieid) from posters P where P.movieid=1),
(select count(R.movieid) from reviews R where R.movieid=1),
(select count(T.movieid) from trailers T where T.movieid=1)
The tables are not "really" joined so you need three selects.

Categories