Solved

Update query help

Posted on 2010-08-16
6
268 Views
Last Modified: 2012-05-10
OK, got these tables in my e-commerce system.
CREATE TABLE `ps_feature_value_lang` (
	`id_feature_value` INT(10) UNSIGNED NOT NULL,
	`id_lang` INT(10) UNSIGNED NOT NULL,
	`value` VARCHAR(255) NULL DEFAULT NULL,
        <SNIP INDEXES>
)

CREATE TABLE `ps_feature_product` (
	`id_feature` INT(10) UNSIGNED NOT NULL,
	`id_product` INT(10) UNSIGNED NOT NULL,
	`id_feature_value` INT(10) UNSIGNED NOT NULL,
        <SNIP INDEXES>
)

CREATE TABLE `ps_product` (
	`id_product` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`id_supplier` INT(10) UNSIGNED NULL DEFAULT NULL,
	`id_manufacturer` INT(10) UNSIGNED NULL DEFAULT NULL,
        <SNIP columns>
	`reference` VARCHAR(32) NULL DEFAULT NULL,
        <SNIP more columns>
        <SNIP INDEXES>
)

Open in new window


I need to update the ps_product.reference value with the ps_feature_value_lang.value where the ps_feature_value_lang.id_lang = 1 and ps_feature_product.id_product = ps_product.id_product

This query isn't doing what I expected, it replaced them with one single value.
UPDATE ps_product p 
SET p.reference = 
  (SELECT fvl.value FROM ps_feature_value_lang fvl
   INNER JOIN ps_feature_product fp ON fp.id_feature_value = fvl.id_feature_value
   WHERE fp.id_feature = 1 
   AND   fp.id_product = p.id_product 
   AND   fvl.id_lang = 1
   AND   TRIM(p.reference)='')

Open in new window

0
Comment
Question by:EddieShipman
  • 3
  • 3
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33449320
I presume you want this:
UPDATE ps_product p 

SET p.reference = 

  (SELECT group_concat(fvl.value) FROM ps_feature_value_lang fvl

   INNER JOIN ps_feature_product fp ON fp.id_feature_value = fvl.id_feature_value

   WHERE fp.id_feature = 1 

   AND   fp.id_product = p.id_product 

   AND   fvl.id_lang = 1

   AND   TRIM(p.reference)='')

Open in new window

0
 
LVL 26

Author Comment

by:EddieShipman
ID: 33449604
I'm not sure what group_concat does for me, can you explain? SHouldn't I also be using a DISTINCT in the select, though?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33452171
well, you wrote:
>, it replaced them with one single value.

group_concat will build up a list of all the found values (comma-separated)

>SHouldn't I also be using a DISTINCT in the select, though?
SELECT group_concat(distinct fvl.value)

should do that ...
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 26

Author Comment

by:EddieShipman
ID: 33453474
[quote]group_concat will build up a list of all the found values (comma-separated)[/quote]

Who said I wanted a comma sepearated list of values?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33453507
well, please show with data samples what you need.
0
 
LVL 26

Author Closing Comment

by:EddieShipman
ID: 33454596
I no longer need this as I am reloading my tables from scratch.
Thanks...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now