[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

MySQL Insert

Posted on 2011-03-04
2
Medium Priority
?
392 Views
Last Modified: 2012-05-11
I am have a brain fart trying to figure out the logic to perform a particular query. Looking for assistance!

The following table is used to store all the items that a child has in his chest. As you can see, a child can have multiple items.

child_id     item_id     quantity
1               1              1
2               1              1
2               1              1
2               4              1
2               4              1
3               8              1
3               9              1
4               2              1

What I am trying to do is give every child item #8 that don't already have it. My first attempt at this query is show in the code section below. However, this doesn't work very well, as it will still return records if the child has other options.
INSERT child_items (child_id, item_id, quantity)
SELECT DISTINCT child_id, 8, 1 
FROM child_items WHERE item_id != 8;

Open in new window

0
Comment
Question by:plecostomus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 35038412
what about:
INSERT child_items (child_id, item_id, quantity)
SELECT child_id, 8, 1 
FROM child_items 
GROUP BY child_id
HAVING MAX(CASE WHEN item_id = 8 THEN 1 ELSE 0 END) = 0

Open in new window


or:
INSERT child_items (child_id, item_id, quantity)
SELECT child_id, 8, 1 
FROM child_items i
WHERE NOT EXISTS( SELECT NULL FROM child_items o WHERE o.child_id = i.child_id AND o.item_id = 8 )
GROUP BY child_id

Open in new window


however, not sure if it will actually work in MySQL, you might run into the issue that you cannot select from the same table you do the insert into,
so you might need to run the insert into a temp table first ...

0
 

Author Closing Comment

by:plecostomus
ID: 35038632
Worked perfectly... MySQL 5.0/5.1 allows you to insert into itself from itself.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

656 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