开发者

Calling a variable from a different column or table inside a value

I have a table called ProductInfo in my database. This table contains text descriptions of specific products. Just a text field called description, picture url etc.

I have another table called Locations. In this table I have company names, city names (New York, Boston, Omaha etc.)

What I want to be able to do is insert the variables from the Locations table into the text field for the descriptions in the ProductInfo table.

So.. in my ProductInfo table I have an entry called "Widget" and it has a description. I would like to be able to do the following:

This widget is the best widget money can buy. If you live in the ($city1, $city2, $city3) area then you simply must by this widget from ($company_name). ($company_name) specializes in providing ($service1) for clients living in the ($city 4), ($state) areas.

Can anyone provide some insight as to how I might be able to do t开发者_运维百科his. I guess it would be nesting variables inside other variables, but i am not sure.

Thanks in advance.


Dax, I have very little info to go on, but here's a query that would combine fields from three tables:

Location
  id integer not null autoincrement primary key
  company_id integer not null
  city varchar not null
  FOREIGN KEY company_id REFERENCES company.id ON DELETE CASCADE ON UPDATE CASCADE

Company
  id integer not null autoincrement primary key
  companyname varchar not null
  service varchar not null
  city varchar not null
  state varchar not null
  ....other fields...

ProductInfo
  id integer not null autoincrement primary key
  company_id integer not null
  productname varchar ....
  FOREIGN KEY company_id REFERENCES company.id ON DELETE CASCADE ON UPDATE CASCADE

This query would produce the info that you require.

SELECT group_concat(DISTINCT l.city ORDER BY l.city SEPARATOR ', ') AS city
  , c.companyname
  , c.service
  , c.city
  , c.state
FROM location l
INNER JOIN productInfo pi ON (pi.company_id = l.company_id)
INNER JOIN company c ON (pi.company_id = c.id)
WHERE pi.id = 589
GROUP BY pi.id

The main trick is accomplished by GROUP_CONCAT,
see: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
If you use group_concat you must also include a group by clause.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜