Solved

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

Posted on 2014-07-27
7
349 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
7 Comments
 
LVL 82

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 34

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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

Author Comment

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

7 Experts available now in Live!

Get 1:1 Help Now