1

Hope you had a good christmas!

Im having an issue with a query of mine,

i have this query

SELECT * , group_concat( tt.tradeID ) AS tradeArray
FROM tbl_tradesmen AS t
LEFT JOIN tbl_tradesmen_trades AS tt ON tt.tradesmenID = t.tradesmenID
LEFT JOIN tbl_trades AS trade ON trade.tradeID = tt.tradeID
LEFT JOIN tbl_tradesmen_location AS loc ON loc.tradesmenID = t.tradesmenID
LEFT JOIN tbl_locations AS l ON l.locationID = loc.locationID
GROUP BY t.tradesmenID

which returns 1 record, which is what i expect as i have 1 tradesmen in the tbl_tradesmen table. However this tradesmen has 2 trades in the tbl_tradesmen_trades table and the group_concat field contains both of these.

now i want to find out the number of records so i thought the best way is to add a count to my query like so

SELECT COUNT(*) , group_concat( tt.tradeID ) AS tradeArray
FROM tbl_tradesmen AS t
LEFT JOIN tbl_tradesmen_trades AS tt ON tt.tradesmenID = t.tradesmenID
LEFT JOIN tbl_trades AS trade ON trade.tradeID = tt.tradeID
LEFT JOIN tbl_tradesmen_location AS loc ON loc.tradesmenID = t.tradesmenID
LEFT JOIN tbl_locations AS l ON l.locationID = loc.locationID
GROUP BY t.tradesmenID

But this is returning a count value of 2 and not 1 but im not sure why this is or how to get it to return 1.

any help will be greatly appreciated Thanks

3
  • Why do you use Group By if you get 1 record anyway? Commented Dec 30, 2013 at 22:34
  • i only have test data in my database at the moment so i have 1 tradesmen with 2 trades, and i expect my query to return a count of 1 as there is 1 tradesmen but it was returning 2? then i was helped out by a member on here whos comments have disappeared :( and i got it to return a count of 1 which was great now i've added another tradesmen but still the count is 1 :( as the guy who helped me before said it would. should i remove the group by? thanks Commented Dec 30, 2013 at 22:55
  • I am not sure what you are after, but it looks like you want to SELECT t.tradesmenID, group_concat(tt.tradeID)....Group By t.tradesmenID. Commented Dec 30, 2013 at 23:24

1 Answer 1

1

Try this and u will understand what happening

SELECT *
FROM tbl_tradesmen AS t
LEFT JOIN tbl_tradesmen_trades AS tt ON tt.tradesmenID = t.tradesmenID
LEFT JOIN tbl_trades AS trade ON trade.tradeID = tt.tradeID
LEFT JOIN tbl_tradesmen_location AS loc ON loc.tradesmenID = t.tradesmenID
LEFT JOIN tbl_locations AS l ON l.locationID = loc.locationID

You are using JOIN to merge tradesmen with trades so if your trader men is "Jon" and make trade 1 and 2 u will get something like this:

Trader Trade
Jon    1
Jon    2

So when u make a group by t.tradesmenID you will get 1 row, because you hav only 1 tradesmen but there are 2 rows so Count give u a result value of 2

I hope this help you, sorry for my english.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.