Solved

SQL Insert Query Help, part2

Posted on 2016-10-05
6
70 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 500 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

717 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