Solved

mySQL query needed:  find an ID where two (meta_key) rows match

Posted on 2014-07-27
7
363 Views
Last Modified: 2014-07-28
there's one meta table   tblOrdersMeta   with various meta_keys per orderitem_ID

tableID    orderitem_ID   meta_key     meta_value
     1               10                    productID        123
     2               10                    orderID             456

what is the query to find the orderitem_ID  (ex: 10)   that has in common  productID=123   and    orderID=456
(tableID is irrelevant for the end result)

(i can put in the appropriate PHP variables etc)
0
Comment
Question by:willsherwood
[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
7 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40222951
If you know the orderitem_ID, then this.

SELECT * FROM tblOrdersMeta WHERE orderitem_ID = 10;

If not then you need to do a query on one of the other items to get it.
0
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 200 total points
ID: 40223011
Try this (untested):
SELECT orderitem_ID FROM 
(
(SELECT orderitem_ID, meta_key, meta_value FROM tblOrdersMeta 
WHERE meta_key = 'productID' AND meta_value = '123') t1
JOIN 
(SELECT orderitem_ID, meta_key, meta_value FROM tblOrdersMeta 
WHERE meta_key = 'orderID' AND meta_value = '456') t2
ON t1.orderitem_ID=t2.orderitem_ID
)

Open in new window

The idea is to select all the rows with productID=123, then all the rows with orderID=456, then INNER JOIN them together on orderitem_ID, which will give you all the common orderitem_ID.

HTH,
Dan
0
 

Author Comment

by:willsherwood
ID: 40223019
orderitemID is what i'm looking for.
something like:

WHERE
      (select orderitemID FROM...  WHERE   meta_key='productID' AND meta_value=123 )
=
      (select orderitemID FROM...  WHERE   meta_key='orderID' AND     meta_value=456 )
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:willsherwood
ID: 40223022
Dan - i'll give it a try (my reply to Dave crossed paths)
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 40223079
If you want to get a little bit of PHP into the mix you might try this strategy.

1. SELECT orderitem_id and create an array of all of them.
2. Use array_not_unique() to find the replicants.
3. Iterate over the replicants to SELECT the rows with replicated orderitem_id values.

<?php // demo/array_not_unique.php
error_reporting(E_ALL);
echo "<pre>";

// A FUNCTION TO FIND REPLICATED VALUES IN AN ARRAY
function array_not_unique($raw)
{
    // MAN PAGE: http://php.net/manual/en/function.array-count-values.php
    $new = array_count_values($raw);
    foreach ($new as $key => $val)
    {
        if ($val < 2) unset($new[$key]);
    }
    return $new;
}


// SOME TEST DATA
$raw_array   = array();
$raw_array[] = 'abc@xyz.com';
$raw_array[] = 'def@xyz.com';
$raw_array[] = 'ghi@xyz.com';
$raw_array[] = 'jkl@xyz.com';
$raw_array[] = 'mno@xyz.com';
$raw_array[] = 'pqr@xyz.com';
$raw_array[] = 'stu@xyz.com';

// SOME DUPLICATES
$raw_array[] = 'abc@xyz.com';
$raw_array[] = 'jkl@xyz.com';
$raw_array[] = 'abc@xyz.com';
$raw_array[] = 'def@xyz.com';


// SHOW THE FUNCTION AT WORK
$common = array_not_unique($raw_array);
foreach ($common as $x => $n)
{
    echo PHP_EOL . "THE VALUE $x APPEARED $n TIMES";
}

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 200 total points
ID: 40223167
http://sqlfiddle.com/#!2/b0050/1

SELECT t1.orderitem_ID from tblOrdersMeta t1
inner join tblOrdersMeta t2
ON t1.orderitem_ID=t2.orderitem_ID
where t1.meta_key='productID' 
AND t1.meta_value=123
AND t2.meta_key='orderID' 
AND t2.meta_value=456

Open in new window

0
 

Author Closing Comment

by:willsherwood
ID: 40224570
thanks all!
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Incorrect definition of table mysql.proc 7 110
Combining Queries 7 54
Unwanted output from my query 5 53
mysql vs miscrosoft sql server 6 62
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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