?
Solved

SQL Insert Query Help, part2

Posted on 2016-10-05
6
Medium Priority
?
72 Views
Last Modified: 2016-10-05
This is related to the schema/sample data already posted on this question:
here


I have 2 rows that need to be added to a table, `wp_posts` that have a count for the number of contacts for a given post_id.   One row will have a meta_key of 'contacts' and the meta_value will be the count.  The 2nd row meta_key will be '_contacts' and meta_value will always be 'field_5790aaada7333'.  The post_id will be the same for both rows.  So for example, using the sample data posted on the other question, the rows would look like this when finished:
post_id 	meta_key 	meta_value
3001 	contacts 	5
3001 	_contacts 	field_5790aaada7333
3003 	contacts 	1
3003 	_contacts 	field_5790aaada7333
3005 	contacts 	2
3005 	_contacts 	field_5790aaada7333
3008 	contacts 	4
3008 	_contacts 	field_5790aaada7333

Open in new window


The other issue with this is that the rows may exist for a given already, and if so only the first row need to be updated with the actual number of contacts.  My thought was to count the number of rows where the meta_key is like one of the meta_keys that is static, i.e. field_5790aac3a7334, for a given post_id, however I am unsure of how to either update the value of a row or insert 2 new rows if the rows do not exist.
0
Comment
Question by:bdhtechnology
[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
  • 3
  • 3
6 Comments
 
LVL 1

Author Comment

by:bdhtechnology
ID: 41829700
Actually there are only 7 rows where the fields exist already, so the part regarding the update portion of the query can be ignored, I just need to add 2 rows for each post_id to count the number of contacts.
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 41829704
can this be a separate query?

I am about to finish up for the night, so thi s is as far as my sleepy eyes would let me go.. hope it helps

SELECT
...
  CASE
         WHEN cj.n = 1 THEN c.contact_count
         WHEN cj.n = 2 THEN 'field_5790aaada7333'
  END AS meta_value
FROM (
    SELECT
         cc.company
       , COUNT(*) contact_count
    FROM `chambercontacts` cc
    GROUP BY
         cc.company
      ) c
...
CROSS JOIN (
          SELECT 1 n UNION ALL
          SELECT 2 n
         ) cj
    

Open in new window

0
 
LVL 1

Author Comment

by:bdhtechnology
ID: 41829708
Yes, I have the rows inserted now but to use them I need to have the correct count for the actual number of contacts.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 49

Expert Comment

by:PortletPaul
ID: 41829717
please refresh page, my lat comment was being changed as you added :)

I'm off to bed I'm afraid way too tired
0
 
LVL 1

Author Comment

by:bdhtechnology
ID: 41830989
That was quite helpful, with a few tweaks I was able to finish this up.  Thanks for all of your help!

INSERT INTO `wp_postmeta` (`post_id`, `meta_key`, `meta_value`)
SELECT
  p.ID AS post_id,
  CASE
         WHEN cj.n = 1 THEN 'contacts'
         WHEN cj.n = 2 THEN '_contacts'
  END AS meta_key,
  CASE
         WHEN cj.n = 1 THEN c.contact_count
         WHEN cj.n = 2 THEN 'field_5790aaada7333'
  END AS meta_value
FROM (
  SELECT
     cc.company,
     cc.deleted,
     COUNT(*) contact_count
  FROM `chambercontacts` cc
  WHERE cc.deleted=0
  GROUP BY
       cc.company
  ) c
  LEFT JOIN `directory` d ON c.company=d.id
  LEFT JOIN `wp_posts` p ON d.companyname LIKE p.post_title
  CROSS JOIN (
          SELECT 1 n UNION ALL
          SELECT 2 n
  ) cj
WHERE
  d.deleted=0 AND
  p.ID>=3000;

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41831084
Great! Thanks for the question(s). Cheers, Paul
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
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…

741 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