Hi guys!
I have a challenge that I hope you can help me with. I have three
tables and I have exported them with some sample data and uploaded
them here:
http://server.arton.no/filesharing/SQL.zip
Here is how the tables looks like:
"zc_artonproject_content". Fields: contentId, projectId, img, name,
ordering
"zc_artonproject_tags" Fields: id, title, inmenu, visible, ordering
"zc_artonproject_tagrelation" Fields: tagId, contentId, projectId
What I want to do is to get "tagId", "contentId", "projectId" from the
"zc_artonproject_tagrelation", and "img" from the
"zc_artonproject_content", and "title" from "zc_artonproject_tags"
selected by a tagId.
So for example let's say that I use '1' as tagId, this is my attempt
so far:
// get the tagId's I want to exclude
SELECT id FROM zc_artonproject_tags WHERE id != 1 AND inmenu = 1 OR
visible = 0";
I load this as an array in PHP and assign this to the variable
$excludedTags and use it in the next query:
SELECT tr.tagId, tr.contentId, tr.projectId, c.img, t.title
FROM zc_artonproject_tagrelation tr, zc_artonproject_content c,
zc_artonproject_tags t"
WHERE tr.contentId = c.contentId
AND tr.tagId = t.id
AND tr.contentId IN (SELECT contentId
FROM zc_artonproject_tagrelation
WHERE tagId = 1
AND tagId NOT IN('". implode( ',',
$excludedTags) ."'))
Now I get all the rows where contentId has tagId 1 except the tagId's
I want to exclude from the result. Then I use PHP to create an array
with nested arrays sorted by tagId containing objects with the data,
and then sort the nested arrays by their length.
The result is pretty close to what I want. But as you can see below
the result have multiple rows with the same projectId and tagId, and I
want only one row per projectId with the same tagId. For example look
at the result below. You can see that in the first array the projectId
"509" is repeated with tagId "1" four times. I want only one row. The
same goes for the second array where projectId "509" is repeated with
tagId "10" four times, and again here I want only one. I could create
a loop in PHP to remove all the duplicates, but I'm sure this is more
appropriate to do this in the mysql query both by readability and
performance. I'm also curious to see how this can be done in mysql
only.
Can you fine people point me in the right direction, or even better
give me an example on how to do this?
Thanks in advance!
Best regards Thomas
PS: I've tried to explain this as good as I can, but please don't
hesitate to ask if you have any questions!
My result so far:
# [array] 0
* [stdClass object] 0
Properties
+ [string] tagId = "1"
+ [string] contentId = "22"
+ [string] projectId = "543"
+ [string] img = "perspektiv2.jpg"
+ [string] title = "kontor"
* [stdClass object] 1
Properties
+ [string] tagId = "1"
+ [string] contentId = "90"
+ [string] projectId = "547"
+ [string] img = "mte7.jpg"
+ [string] title = "kontor"
* [stdClass object] 2
Properties
+ [string] tagId = "1"
+ [string] contentId = "95"
+ [string] projectId = "509"
+ [string] img = "front-side.jpg"
+ [string] title = "kontor"
* [stdClass object] 3
Properties
+ [string] tagId = "1"
+ [string] contentId = "96"
+ [string] projectId = "509"
+ [string] img = "front.jpg"
+ [string] title = "kontor"
* [stdClass object] 4
Properties
+ [string] tagId = "1"
+ [string] contentId = "97"
+ [string] projectId = "509"
+ [string] img = "side.jpg"
+ [string] title = "kontor"
* [stdClass object] 5
Properties
+ [string] tagId = "1"
+ [string] contentId = "99"
+ [string] projectId = "509"
+ [string] img = "fr-web.jpg"
+ [string] title = "kontor"
* [stdClass object] 6
Properties
+ [string] tagId = "1"
+ [string] contentId = "100"
+ [string] projectId = "447"
+ [string] img = "resepsjon2.jpg"
+ [string] title = "kontor"
* [stdClass object] 7
Properties
+ [string] tagId = "1"
+ [string] contentId = "111"
+ [string] projectId = "438"
+ [string] img = "peis5.jpg"
+ [string] title = "kontor"
* [stdClass object] 8
Properties
+ [string] tagId = "1"
+ [string] contentId = "113"
+ [string] projectId = "438"
+ [string] img = "kantine11.jpg"
+ [string] title = "kontor"
* [stdClass object] 9
Properties
+ [string] tagId = "1"
+ [string] contentId = "114"
+ [string] projectId = "438"
+ [string] img = "mte10.jpg"
+ [string] title = "kontor"
# [array] 1
* [stdClass object] 0
Properties
+ [string] tagId = "10"
+ [string] contentId = "22"
+ [string] projectId = "543"
+ [string] img = "perspektiv2.jpg"
+ [string] title = "fasade"
* [stdClass object] 1
Properties
+ [string] tagId = "10"
+ [string] contentId = "95"
+ [string] projectId = "509"
+ [string] img = "front-side.jpg"
+ [string] title = "fasade"
* [stdClass object] 2
Properties
+ [string] tagId = "10"
+ [string] contentId = "96"
+ [string] projectId = "509"
+ [string] img = "front.jpg"
+ [string] title = "fasade"
* [stdClass object] 3
Properties
+ [string] tagId = "10"
+ [string] contentId = "97"
+ [string] projectId = "509"
+ [string] img = "side.jpg"
+ [string] title = "fasade"
* [stdClass object] 4
Properties
+ [string] tagId = "10"
+ [string] contentId = "99"
+ [string] projectId = "509"
+ [string] img = "fr-web.jpg"
+ [string] title = "fasade"
# [array] 2
* [stdClass object] 0
Properties
+ [string] tagId = "11"
+ [string] contentId = "111"
+ [string] projectId = "438"
+ [string] img = "peis5.jpg"
+ [string] title = "kantine"
* [stdClass object] 1
Properties
+ [string] tagId = "11"
+ [string] contentId = "113"
+ [string] projectId = "438"
+ [string] img = "kantine11.jpg"
+ [string] title = "kantine"
* [stdClass object] 2
Properties
+ [string] tagId = "11"
+ [string] contentId = "114"
+ [string] projectId = "438"
+ [string] img = "mte10.jpg"
+ [string] title = "kantine"
# [array] 3
* [stdClass object] 0
Properties
+ [string] tagId = "12"
+ [string] contentId = "90"
+ [string] projectId = "547"
+ [string] img = "mte7.jpg"
+ [string] title = "møterom"
* [stdClass object] 1
Properties
+ [string] tagId = "12"
+ [string] contentId = "114"
+ [string] projectId = "438"
+ [string] img = "mte10.jpg"
+ [string] title = "møterom"
# [array] 4
* [stdClass object] 0
Properties
+ [string] tagId = "4"
+ [string] contentId = "100"
+ [string] projectId = "447"
+ [string] img = "resepsjon2.jpg"
+ [string] title = "resepsjon"