The subquery limits results perfectly when the WHERE clause at the end of the statement is not included. I understand that the subquery LIMIT happens first then the WHERE clause is fired on that result set and that this is a limitation/restriction of a subquery.
What I need is someone more experienced than me to help a brother out with retrieving the records with a LIMIT with the ability to restrict that result set by the WHERE clauses. Let me know if this was not explained well enough.
I also scoured the interwebs in search of the answer for hours with no luck. Your time is appreciated.
EDIT: added a crude example on SQLfiddle: http://sqlfiddle.com/#!9/2de563/4
SELECT *
FROM (SELECT * FROM parent_products LIMIT 10) pp
INNER JOIN products_variants pv ON pv.parent_id=pp.parent_id
INNER JOIN products p ON p.id=pv.product_id
INNER JOIN product_types pt ON pt.product_type_id=p.product_type
LEFT JOIN team_list t ON pp.team_id=t.team_id
LEFT JOIN photos ph ON ph.product_id=p.id
LEFT JOIN product_attributes pa ON pa.product_id=pv.product_id
LEFT JOIN attributes a ON a.id=pa.attribute_id
LEFT JOIN product_attribute_options po ON po.product_attribute_option_id=a.parent_id
WHERE t.team_id=100 AND p.active='y';
Explain select:
Below query will give you the expected output
SELECT * FROM (SELECT users.id,users.name FROM users
LEFT JOIN map ON users.id=map.user_id
LEFT JOIN locations ON locations.location_id=map.location_id
INNER JOIN type ON locations.type=type.id
WHERE type.id=1 limit 3) users
LEFT JOIN map ON users.id=map.user_id
LEFT JOIN locations ON locations.location_id=map.location_id
INNER JOIN type ON locations.type=type.id
where type.id=1;
Related
I'm using mysql and I confused with "And", "Where"
Somby dy can tell me what is difference between these.
SELECT *,COUNT(comment.id) as comment_count from posts LEFT JOIN comment on posts.post_id =comment.post_id AND comment.approve = 1 GROUP BY posts.post_id
SELECT *,COUNT(comment.id) as comment_count from posts LEFT JOIN comment on posts.post_id =comment.post_id WHERE comment.approve = 1 GROUP BY posts.post_id
They are not the same, first one will return the associations for all, and the second will do it just for the rows in the where match.
In this other duplicate question you can see the full explanation and examples
SQL JOIN - WHERE clause vs. ON clause
Simply change the query to use an inner join like this:
select tableA.id, tableA.name, tableB.details
from tableA
inner join tableB ...
here is the definition of left join:
The LEFT JOIN (also called LEFT OUTER JOIN) keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).
whereas the definition of the inner join is:
The INNER JOIN keyword return rows when there is at least one match in both tables.
Can someone help me to understand those results ? (For me all 3 should return 6455).
(Using RDS mysql-8.0.13)
SELECT COUNT(p.product_id) FROM product p LEFT JOIN product_attributes pa ON p.pdt_id = pa.pdt_id WHERE pa.code = 'season';
Results : 6332
SELECT COUNT(*) FROM product p;
Results : 6455
SELECT COUNT(p.product_id) FROM product p LEFT JOIN product_attributes pa ON p.pdt_id = pa.pdt_id AND pa.code = 'season';
Results : 6455
Your first join uses the WHERE clause, this mean sit selected all the rows, including those with a null join and then filters out those WHERE the pa.code = season, i.e. the null joins.
The last one joins on both, but because it is a left join you still get the full table of results, and nothing is filtered because you remove the WHERE clause. If you were to use an INNER JOIN in the last query you should get the same result (6332).
This link might be useful What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
I have a table let's call it products with a list of Manufacturers and Products.
I have a second table let's call it Customer, Orders.
I can do a join to make a list of all the items from each manufacturer the customer ordered doing an Inner Join. Yet trying to do an Inner Join for the items they did not fails.
I tried an Inner Join with 'Orders.Product != Products.Product' but that only works where the Customer has one order. Once there is more than one order I get the same list I would have doing an Inner Join. Any thoughts? I'll try to make a SqlFiddle tonight but was hoping a quick description might help a MySql / Join expert who has done 'NOT Inner Join'before...
It is called an anti join, you can use left join with is null check:
select p.*
from products p
left join orders o on p.Product = o.Product
where o.product is null
I have three tables People, Items and Locations. People can have only 1 Items. Locations has no relation to any of 2 tables. I want to get I record join all 3. I did 2 so far people + items but 3rd I keep getting MySQL errors. There's no JOIN ON for location. Any help?
SELECT * FROM ITEMS i
RIGHT JOIN PEOPLE p
ON (p.ITEM_ID =i.ID) where
p.ID=3
RIGHT JOIN
SELECT * FROM LOCATIONS lo where lo.ID=7
If there are no join keys in common, then you might want to do a cross join. This produces a Cartesian product, that is, every location for each row selected from People/items:
SELECT *
FROM ITEMS i RIGHT JOIN
PEOPLE p
ON (p.ITEM_ID =i.ID) cross join
location l
WHERE p.ID=3
By the way, MySQL has a very flexible (and non-standard) join syntax. You can actually leave the on clause off of a join and it will behave the same as a cross join. That is a bad habit, of course. If you want a cross join, then use cross join explicitly.
I'm assuming you want the location with the ID of 7 to appear on every row...
SELECT *
,(SELECT loc.name FROM Location loc WHERE loc.ID = 7) AS Location
FROM ITEMS i
RIGHT JOIN PEOPLE p
ON (p.ITEM_ID =i.ID)
WHERE p.ID=3
OR
SELECT *
FROM ITEMS i
RIGHT JOIN PEOPLE p
ON (p.ITEM_ID =i.ID)
CROSS JOIN (SELECT * FROM LOCATIONS lo where lo.ID=7) l
WHERE p.ID=3
OR
[several other ways to go about it]
Try something like this:
SELECT peo.id, it.id, loc.id
FROM People as peo
INNER JOIN Items as it on it.id = peo.id
INNER JOIN Locations as loc on loc.id = peo.id
WHERE peo.ID=3
Edit:
Your question was edited while I was typing this so my example doesn't match like it used to. Use ITEM_ID and ID as needed.
Although not recommended, you can also use
SELECT *
FROM People as peo
INNER JOIN Items as it on it.id = peo.id
INNER JOIN Locations as loc on loc.id = peo.id
WHERE peo.ID=3
Hey guys quick question, I always use left join, but when I left join twice I always get funny results, usually duplicates. I am currently working on a query that Left Joins twice to retrieve the necessary information needed but I was wondering if it were possible to build another select statement in so then I do not need two left joins or two queries or if there were a better way. For example, if I could select the topic.creator in table.topic first AS something, then I could select that variable in users and left join table.scrusersonline. Thanks in advance for any advice.
SELECT * FROM scrusersonline
LEFT JOIN users ON users.id = scrusersonline.id
LEFT JOIN topic ON users.username = topic.creator
WHERE scrusersonline.topic_id = '$topic_id'
The whole point of this query is to check if the topic.creator is online by retrieving his name from table.topic and matching his id in table.users, then checking if he is in table.scrusersonline. It produces duplicate entries unfortunately and is thus inaccurate in my mind.
You use a LEFT JOIN when you want data back regardless. In this case, if the creator is offline, getting no rows back would be a good indication - so remove the LEFT joins and just do regular joins.
SELECT *
FROM scrusersonline AS o
JOIN users AS u ON u.id = o.id
JOIN topic AS t ON u.username = t.creator
WHERE o.topic_id = '$topic_id'
One option is to group your joins thus:
SELECT *
FROM scrusersonline
LEFT JOIN (users ON users.id = scrusersonline.id
JOIN topic ON users.username = topic.creator)
WHERE scrusersonline.topic_id = '$topic_id'
Try:
select * from topic t
left outer join (
users u
inner join scrusersonline o on u.id = o.id
) on t.creator = u.username
If o.id is null, the user is offline.
Would not it be better to match against topic_id in the topics table by moving the condition to the join. I think it will solve your problem, since duplicates come from joining with the topics table:
SELECT * FROM scrusersonline
JOIN users
ON users.id = scrusersonline.id
LEFT JOIN topic
ON scrusersonline.topic_id = '$topic_id'
AND users.username = topic.creator
By the way, LEFT JOIN with users is not required since you seem to search for the intersection between scrusersonline and users