i have following data (sample) for template table
TempName, ItemID
Test1, 1/5/6/8/11
Test2, 2/4/5/8/10
So, i like to make a sql query that will join with Item Tables And Return All the rows of data that matched/exists in ItemID Field
Something like: Select * From Items Inner Join Template On Items.ID = Template.ItemID Where Template Name = 'Test1';
i can't figure correct sql query so, now i am using a for loop and but i prefer one liner or better query/script to handle that.
currently my code is like:
Select ItemID From Template Where Name = 'Test1';
$strItems = explode('/', ItemID)
foreach($strItem in $strItems)
{
select * from Items where ID = $strItem
}
that's not the pretty/well formatted code, but to show how i am working now.. but i am looking for a better approach :)
best regards
You may use FIND_IN_SET
here, with a single query:
SELECT i.*
FROM Items i
INNER JOIN Template t
ON FIND_IN_SET(i.ID, REPLACE(t.ItemID, '/', ',')) > 0
WHERE
t.Name = 'Test1';
But note that storing unnormalized data as you have done in the Template
table is not idea at all. It would be much better to store the ItemID
values across separate rows.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.