I need to build a query with multiple JOIN, to be more especific, 2 JOINS, but it gets duplicated results, check this:
My current tables:
food_shops
id, slug, name
categories_food_shops
id, id_cat, id_food_shop
pictures_food_shops
id, pic_slug, id_food_shop
And I need to get * from food_shops, the id_cat from categories_food_shops and pic_slug from pictures_food_shops...
My current query is like this:
SELECT food_shops.id, food_shops.slug, food_shops.name,
GROUP_CONCAT(categories_food_shops.id_category) as categories,
GROUP_CONCAT(pictures_food_shops.slug) as pictures
FROM
food_shops
JOIN categories_food_shops
ON food_shops.id = categories_food_shops.id_food_shop
JOIN pictures_food_shops
ON food_shops.id = pictures_food_shops.id_food_shop
GROUP BY food_shops.slug, pictures_food_shops.id_food_shop
But since my pictures_food_shops have more results as the categories_food_shops, my result is gettin "quadruplicated":
What can I do to prevent this and get only the correct amount of categories?
Only 1 at the first row, 3 and 5 in the second one and 7,1,6 at the last one?
Thanks!
You can try this:
...
GROUP_CONCAT(DISTINCT categories_food_shops.id_category) as categories,
....
This should work for pictures also.
Here is the documentation with DISTINCT usage example.
Related
When I type the following query
SELECT
COUNT(*) AS COUNT
FROM
OP_table OP
WHERE
OP.TARGET_ID= 4330000000000369;
I get a count of 55
When I try to use it in a join
SELECT
TS.TARGET_ID, T.TARGET_NAME, T.TARGET_PUBLIC_NAME, count( DISTINCT OP.OP_ID) AS OP_COUNT
FROM
TS_table TS
INNER jOIN
T_table T
ON
T.TARGET_ID = TS.TARGET_ID
OUTER JOIN
OP_TABLE OP
ON
OP.TARGET_ID = T.TARGET_ID
WHERE
TS.TARGET_SERVICE_ID = number
Then I get
TARGET_ID, TARGET_NAME, TARGET_PUBLIC_NAME, OP_COUNT
number, target name, Ebook Central History 33781
with an count of 33781. I want to use the 2nd functions structure but get the right count of 55
for some reason it's getting the count of the Target_id's instead of Target_service ID's
also I noticed if I type the following
SELECT
COUNT(*) AS COUNT
FROM
KB_OBJECT_PORTFOLIOS OP
WHERE
OP.TARGET_ID=4330000000000383;
where that number corresponds will produce the result 33781.
somewhere within the joining it's getting rid of the target ID of 4330000000000369 and using a different target ID 4330000000000383
Ok, I get mixed up from the stupid column names OP.target_ID = TS.target_service_id and not TS.target_ID...
I need to write query that joins several tables and I need distinct value from one table based on max count().
These are my tables names and columns:
bands:
db|name|
releases_artists:
release_db|band_db
releases_styles
release_db|style
Relations between tables are (needed for JOINs):
releases_artists.band_db = bands.db
releases_styles.release_db = releases_artists.release_db
And now the query that I need to write:
SELECT b.name, most_common_style
LEFT JOIN releases_artists ra ON ra.band_db = b.db
and here I need to find the most common style from all band releases
JOIN(
SELECT DISTINCT style WHERE releases_styles.release_db = ra.release_db ORDER BY COUNT() DESC LIMIT 1
)
FROM bands b
WHERE b.name LIKE 'something'
This is just a non working example of what I want to accomplish. It would be great if someone could help me build this query.
Thanks in advance.
EDIT 1
Each artist from table bands can have multiple records from releases_artists table based on band_db and each release can have multiple styles from releases_styles based on release_db
So if I search for b.name LIKE '%ray%' it returns something similar to:
`bands`:
o7te|Ray Wilson
9i84|Ray Parkey Jr.
`releases_artists` for Ray Wilson:
tv5c|o7te (for example album `Change`)
78wz|o7te (`The Next Best Thing`)
nz7c|o7te (`Propaganda Man`)
`releases_styles`
tv5c|Pop
tv5c|Rock
tv5c|Alternative Pop/Rock
----
78wz|Rock
78wz|Pop
78wz|Classic Rock
I need style name that repeats mostly from all artist releases as this artist main style.
Ok, this is a bit of a hack. But the only alternatives I could think of involve heaps of nested subqueries. So here goes:
SELECT name
, SUBSTRING_INDEX(GROUP_CONCAT(style ORDER BY release_count DESC SEPARATOR '|'), '|', 1) AS most_common_style
FROM (
SELECT b.db
, b.name
, rs.style
, COUNT(*) AS release_count
FROM bands b
JOIN releases_artists ra ON ra.band_db = b.db
JOIN releases_styles rs ON rs.release_db = ra.release_db
GROUP BY b.db, rs.style
) s
GROUP BY db;
I have two select:
SELECT ID, ID_cat, modello
FROM tbArticoli
WHERE ID_cat=5
Example result in Json:
{"ID":"5","ID_cat":"5","modello":"Hawaii"},
{"ID":"6","ID_cat":"5","modello":"T-Shirt Righe"},
{"ID":"7","ID_cat":"5","modello":"Polo"},
{"ID":"8","ID_cat":"5","modello":"Fantasia"},
{"ID":"9","ID_cat":"5","modello":"Fiori"},
{"ID":"10","ID_cat":"5","modello":"Arcobaleno"},
{"ID":"11","ID_cat":"5","modello":"Oro"},
{"ID":"12","ID_cat":"5","modello":"Argento"},
{"ID":"13","ID_cat":"5","modello":"StelleStrisce"}
And another select:
SELECT IDModello,
FLOOR(AVG(voto)) AS votomedio
FROM tbCommenti
GROUP BY IDModello
with result:
{"IDModello":"5","votomedio":"7"},
{"IDModello":"6","votomedio":"7"},
{"IDModello":"7","votomedio":"8"},
{"IDModello":"8","votomedio":"6"}
I need a final result like this:
{"ID":"5","ID_cat":"5","modello":"Hawaii","votomedio":"7"},
{"ID":"6","ID_cat":"5","modello":"T-Shirt Righe","votomedio":"7"},
{"ID":"7","ID_cat":"5","modello":"Polo","votomedio":"8"},
{"ID":"8","ID_cat":"5","modello":"Fantasia","votomedio":"6"},
{"ID":"9","ID_cat":"5","modello":"Fiori","votomedio":"null"},
{"ID":"10","ID_cat":"5","modello":"Arcobaleno","votomedio":"null"},
{"ID":"11","ID_cat":"5","modello":"Oro","votomedio":"null"},
{"ID":"12","ID_cat":"5","modello":"Argento","votomedio":"null"},
{"ID":"13","ID_cat":"5","modello":"StelleStrisce","votomedio":"null"}
on tbArticoli.ID = tbCommenti.IDModello
Which is the best query?
Thank you.
Assuming you want to join on id = idmodello, you can do something like
SELECT *
FROM (SELECT ID, ID_cat, modello
FROM tbArticoli
WHERE ID_cat=5) AS tbA
LEFT JOIN (SELECT IDModello,
FLOOR(AVG(voto)) AS votomedio
FROM tbCommenti
GROUP BY IDModello) as tbC
ON tbA.ID = tbC.IDModello
You can specify subqueries as what you are selecting from, because MySQL selects from a set of tuples. The table name just specifies that you want all tuples from that table, whereas in the query above you are specifying the specific tuples that you want. The main thing to note in this query is that you must use the 'AS' keyword to specify a temp name for each set of tuples.
Ive got a simple query that is used on a search. My problem is with this query is that as the records in mysql are added everytime there is a transaction, the query returns a list of data when there could only be one or a few more rows instead of a lot more.
SQLFliddle
As you can see here - the query returns a lot of rows, where I want it to return
BLSH103 A001A 31 24/01/2014
Can the qty where the product name & pallet space are the same be summed? And then show the largest date?
just use a sum function on t.Quantity (and a group by clause)
SELECT (t.ProductName) as Pname ,(s.PalletSpace) as PSpace, sum(t.Quantity) as Qty,(t.TransactionDate) as Transac
FROM PalletSpaces s
JOIN ProductTrans t
ON s.PalletSpaceID = t.PalletSpace
WHERE t.ProductName LIKE 'BLSH103' OR s.PalletSpace LIKE 'BLSH103'
group by
Pname,
pSpace,
Transac -- if you want to group by date also...
By the way, using LIKE this way (without %) doesn't make much sense...
see SqlFiddle
You just need to use GROUP BY and SUM in this way:
SELECT (t.ProductName) as Pname ,(s.PalletSpace) as PSpace, SUM(t.Quantity) as Qty,(t.TransactionDate) as Transac
FROM PalletSpaces s
JOIN ProductTrans t
ON s.PalletSpaceID = t.PalletSpace
WHERE t.ProductName LIKE 'BLSH103' OR s.PalletSpace LIKE 'BLSH103'
GROUP BY t.ProductName, s.PalletSpace;
I have the following query:
SELECT routes.route_date, time_slots.name, time_slots.openings, time_slots.appointments
FROM routes
INNER JOIN time_slots ON routes.route_id = time_slots.route_id
WHERE route_date
BETWEEN 20140109
AND 20140115
AND time_slots.openings > time_slots.appointments
ORDER BY route_date, name
This works just fine and will produce the following results:
What I want to do is only return one name per date. So the 9th, name = 1, would only have 1 result, rather than 2, as it currently does.
UPDATE: See the SQLFIDDLE for different type of solutions here: http://sqlfiddle.com/#!2/9ac65b/6
Will it solve your request if you use...
SELECT DISTINCT routes.route_date...your query... ?
It depends if you know that your rows always will have the same values, for same date/name.
Otherwise use group by...
(which I think suits your request best)
SELECT routes.route_date, time_slots.name, sum(time_slots.openings), sum(time_slots.appointments)
FROM routes
INNER JOIN time_slots ON routes.route_id = time_slots.route_id
WHERE route_date
BETWEEN 20140109
AND 20140115
AND time_slots.openings > time_slots.appointments
group by routes.route_date, time_slots.name
ORDER BY route_date, name
(i did a sum for the openings and appointments, you could do min, max, count, etc. Pick the one that fits your requirements best!)
You need to figure out which "name" you want when there are several for the same date.
Then you can group by date and select the right "name" by using an aggregate function like COUNT, MAX, etc.
I can't help you more if you don't explain your rule for picking one.