(i'm using MySQL 5.0.18)
hello,
first of all, sorry for my english, i'm not native.
an application of our company uses 3 tables (relative linked) to represent "dynamic tables". more exactly there is a table to hold TableName, one to hold attribute names, and one to hold effective data.
an example follows:
the resultant table from tblName = "Guestbook" could than be:
now, the user should have the possibility to set up queries to the resultant table, as if it would be real. he doesn't know about a relative representation of his table in the real db.
a function in the application interprets the WHERE clause of the User Query, and generates a correct query fitting the relative representation of the table.
up to now, this works as long as there are only ORs in the WHERE clause, eg.:
will get..
but as soon as there's an AND in the WHERE Statement, i can't make a selection, because there are more than one resulting row affected.
eg:
i've found a very ugly, stupid and low solution, takes up 12 seconds to resolve:
as you certainly know, the WHERE clause must be like that, because i can't access the subselected attributes.
i know, this is very ugly, and i'm searching for a better solution. please help me to find one! perhaps is there a solution with GROUP BY, or JOIN? i'm not very familiar with this kind of statements.
many many thanks in advance
Jimmy
hello,
first of all, sorry for my english, i'm not native.
an application of our company uses 3 tables (relative linked) to represent "dynamic tables". more exactly there is a table to hold TableName, one to hold attribute names, and one to hold effective data.
an example follows:
Code:
table tblname:
-------------------------
¦ IDtblname ¦ Name ¦
¦=======================¦
¦ 1 ¦ Guestbook ¦
¦-----------------------¦
¦ 2 ¦ Gallery ¦
-------------------------
table tblattr:
---------------------------------------
¦ IDtblattr ¦ IDtblname ¦ Name ¦
¦=====================================¦
¦ 1 ¦ 1 ¦ Nickname ¦
¦-------------------------------------¦
¦ 2 ¦ 1 ¦ E-Mail ¦
¦-------------------------------------¦
¦ 3 ¦ 1 ¦ Message ¦
¦-------------------------------------¦
¦ 4 ¦ 1 ¦ Timestamp ¦
¦-------------------------------------¦
¦ 5 ¦ 2 ¦ ImgSrcSmall ¦
¦-------------------------------------¦
¦ 6 ¦ 2 ¦ ImgSrcBig ¦
¦-------------------------------------¦
¦ 7 ¦ 2 ¦ Comment ¦
---------------------------------------
table tbldata:
----------------------------------------------------------------
¦ IDtbldata ¦ IDtblattr ¦ rowid ¦ data ¦
¦==============================================================¦
¦ 1 ¦ 1 ¦ 1 ¦ Peter ¦
¦--------------------------------------------------------------¦
¦ 2 ¦ 2 ¦ 1 ¦ peter@lala.net ¦
¦--------------------------------------------------------------¦
¦ 3 ¦ 3 ¦ 1 ¦ Hallo, meine Nachricht ... ¦
¦--------------------------------------------------------------¦
¦ 4 ¦ 4 ¦ 1 ¦ 1125518784 ¦
¦--------------------------------------------------------------¦
¦ 5 ¦ 1 ¦ 2 ¦ Jimmy ¦
¦--------------------------------------------------------------¦
¦ 6 ¦ 2 ¦ 2 ¦ jimmy@your.com ¦
¦--------------------------------------------------------------¦
¦ 7 ¦ 3 ¦ 2 ¦ Testnachricht, blabla ... ¦
¦--------------------------------------------------------------¦
¦ 8 ¦ 4 ¦ 2 ¦ 1125910044 ¦
----------------------------------------------------------------
the resultant table from tblName = "Guestbook" could than be:
Code:
-----------------------------------------------------------------------------------
¦ rowid ¦ Nickname ¦ E-Mail ¦ Message ¦ Timestamp ¦
¦=================================================================================¦
¦ 1 ¦ Peter ¦ peter@lala.net ¦ Hallo, meine Nachricht ... ¦ 1125518784 ¦
¦-----------------------------------------------------------------¦---------------¦
¦ 2 ¦ Jimmy ¦ jimmy@your.com ¦ Testnachricht, blabla ... ¦ 1125910044 ¦
-----------------------------------------------------------------------------------
now, the user should have the possibility to set up queries to the resultant table, as if it would be real. he doesn't know about a relative representation of his table in the real db.
a function in the application interprets the WHERE clause of the User Query, and generates a correct query fitting the relative representation of the table.
up to now, this works as long as there are only ORs in the WHERE clause, eg.:
Code:
... WHERE (Name = 'Jimmy') OR (Message LIKE '%Hallo%')
Code:
... WHERE ((IDtblattr = 1) AND (data = 'Jimmy')) OR ((IDtblattr = 3) AND (data LIKE '%Hallo%'))
but as soon as there's an AND in the WHERE Statement, i can't make a selection, because there are more than one resulting row affected.
eg:
Code:
... WHERE (Name = 'Peter') AND (Message LIKE '%Hallo%')
i've found a very ugly, stupid and low solution, takes up 12 seconds to resolve:
Code:
SELECT
DISTINCT tbldata.rowid AS IDrow,
(SELECT data FROM tbldata WHERE (IDtblattr = 1) AND (rowid = IDrow) LIMIT 1) AS `Name`,
(SELECT data FROM tbldata WHERE (IDtblattr = 2) AND (rowid = IDrow) LIMIT 1) AS `E-Mail`,
(SELECT data FROM tbldata WHERE (IDtblattr = 3) AND (rowid = IDrow) LIMIT 1) AS `Message`,
(SELECT data FROM tbldata WHERE (IDtblattr = 4) AND (rowid = IDrow) LIMIT 1) AS `Timestamp`
FROM
`tbldata`
WHERE
rowid IN
(
SELECT rowid FROM tbldata WHERE (IDtblattr=1) AND
(data = 'Peter') AND
(rowid IN
(
SELECT rowid FROM tbldata WHERE (IDtblattr = 3) AND
(data LIKE '%Hallo%')
)
)
)
as you certainly know, the WHERE clause must be like that, because i can't access the subselected attributes.
i know, this is very ugly, and i'm searching for a better solution. please help me to find one! perhaps is there a solution with GROUP BY, or JOIN? i'm not very familiar with this kind of statements.
many many thanks in advance
Jimmy