Welcome Guest, Not a member yet? Register   Sign In
using LIKE in SQL query with activerecord
#6

[eluser]xzela[/eluser]
OK,
Just a heads up, creating a column to store multi-strings of data is not a best way to go about it. As now you are forced to create methods of extracting that data (for loops, if statements, etc...) in your PHP code. It will be very difficult to create a SQL statement to do the same thing. Plus, the next guy who has to maintain your code will hate you forever! Smile

You should really create two tables for this:
tag_table:
-*tag_id //primary_key
-tag_name

product_tag_table:
-*p_tag_id //primary_key
-p_id
-tag_id
(this table actually doesn't need a primary key, but for this example we'll just leave it here)

The product_tag_table holds the relation ship between the products and the tags.

If you have this type of database structure you can use a simple select to get the information you want:
Code:
SELECT
  product_table.name,
  tag_table.tag_name
FROM product_tag_table
LEFT JOIN product_table ON product_tag_table.p_id = product_table.id
LEFT JOIN tag_table ON product_tag_table.tag_id = tag_table.tag_id
WHERE
  product_table.id = 9328;

This will return all of the tags that have a relationship with this product.

Anyway, just a suggestion.

good luck on your site.


Messages In This Thread
using LIKE in SQL query with activerecord - by El Forum - 02-04-2009, 02:11 PM
using LIKE in SQL query with activerecord - by El Forum - 02-04-2009, 04:38 PM
using LIKE in SQL query with activerecord - by El Forum - 02-04-2009, 05:02 PM
using LIKE in SQL query with activerecord - by El Forum - 02-04-2009, 05:24 PM
using LIKE in SQL query with activerecord - by El Forum - 02-04-2009, 06:20 PM
using LIKE in SQL query with activerecord - by El Forum - 02-04-2009, 06:44 PM
using LIKE in SQL query with activerecord - by El Forum - 02-04-2009, 10:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB