Jump to content

Merge 2 SELECT statements


jarvis
Go to solution Solved by Barand,

Recommended Posts

Hi All,

 

I really hope someone can help. Am a novice a this stuff and spent ages getting no where. I hope this makes some sort of sense...

 

I currently have the following MySQL statement:

SELECT pm1.meta_value AS 'ORDER No', pm1.post_id AS 'TICKET No'
FROM $wpdb->postmeta pm, $wpdb->postmeta pm1
WHERE pm.meta_key = '_tribe_wooticket_event'
AND pm.meta_value =263
AND pm1.meta_key = '_tribe_wooticket_order'
AND pm.post_id = pm1.post_id
ORDER BY `pm1`.`post_id` DESC

This returns the following:

 

ORDER No TICKET No
427             430
427              429
427              428
416              424
416              423
416              422
420              421
417              419
417              418
 
Basically, 1 order (427) can contain 3 tickets (430,429,428) - for example!
 
I then have another MySQL query:
SELECT pm.post_id AS 'Order No', pm.meta_value AS 'Guest Name'
FROM $wpdb->postmeta pm
WHERE pm.post_id = '427'
AND pm.meta_key LIKE '% Attendee Name'
LIMIT 0 , 30

This returns the following:

 

Order No  Guest Name
427         John Smith
427         Fred Bloggs
427         AN Other
 
What I need to do, is somehow combine the 2 statements so I end up with 
 
ORDER No  TICKET No   Guest Name
427              430              John Smith
427              429              Fred Bloggs
427              428              AN Other
416              424              Guest A
416              423              Guest B
416              422              Guest C
 
Not sure if it helps but whenever a Name is added, it stores like so:
Meta Key               Meta Value
1 Attendee Name  John Smith
2 Attendee Name  Fred Bloggs
3 Attendee Name  AN Other
 
Any help on this would be much appreciated!
 
Thanks in advanced!!!

 

Link to comment
Share on other sites

I think it should be something like this

SELECT 
    pm1.meta_value AS 'ORDER No'
    , pm1.post_id AS 'TICKET No'
    , pm2.meta_value AS 'Guest Name'
FROM $wpdb->postmeta pm
INNER JOIN $wpdb->postmeta pm1
    ON pm.post_id = pm1.post_id AND pm1.meta_key = '_tribe_wooticket_order'
INNER JOIN $wpdb->postmeta pm2
    ON pm.post_id = pm2.post_id AND pm2.meta_key LIKE '% Attendee Name'
WHERE pm.meta_key = '_tribe_wooticket_event'
AND pm.meta_value = 263
LIMIT 0 , 30

Link to comment
Share on other sites

Hi Barand,

 

Sure, no problem. is this any good:

SELECT * 
FROM $wpdb->postmeta pm, $wpdb->postmeta pm1, $wpdb->postmeta pm2
WHERE pm.meta_key =  '_tribe_wooticket_event'
AND pm.meta_value =263
AND pm1.meta_key =  '_tribe_wooticket_order'
AND pm.post_id = pm1.post_id
AND pm2.meta_key LIKE  '% Attendee Name'
AND pm1.meta_value = pm2.post_id
ORDER BY  `pm1`.`post_id` DESC 
LIMIT 0 , 30

Produces:

 

 

 

 

  meta_id post_id meta_key meta_value meta_id post_id meta_key meta_value meta_id post_id meta_key meta_value 2729 430 _tribe_wooticket_event 263 2728 430 _tribe_wooticket_order 427 2703 427 1 Attendee Name John Smith 2729 430 _tribe_wooticket_event 263 2728 430 _tribe_wooticket_order 427 2706 427 2 Attendee Name Martin Frank 2729 430 _tribe_wooticket_event 263 2728 430 _tribe_wooticket_order 427 2709 427 3 Attendee Name Chris B Bacon 2725 429 _tribe_wooticket_event 263 2724 429 _tribe_wooticket_order 427 2703 427 1 Attendee Name John Smith 2725 429 _tribe_wooticket_event 263 2724 429 _tribe_wooticket_order 427 2706 427 2 Attendee Name Martin Frank 2725 429 _tribe_wooticket_event 263 2724 429 _tribe_wooticket_order 427 2709 427 3 Attendee Name Chris B Bacon 2721 428 _tribe_wooticket_event 263 2720 428 _tribe_wooticket_order 427 2703 427 1 Attendee Name John Smith 2721 428 _tribe_wooticket_event 263 2720 428 _tribe_wooticket_order 427 2706 427 2 Attendee Name Martin Frank 2721 428 _tribe_wooticket_event 263 2720 428 _tribe_wooticket_order 427 2709 427 3 Attendee Name Chris B Bacon 2661 424 _tribe_wooticket_event 263 2660 424 _tribe_wooticket_order 416 2530 416 1 Attendee Name John Smith 2661 424 _tribe_wooticket_event 263 2660 424 _tribe_wooticket_order 416 2533 416 2 Attendee Name Fred Bloggs 2661 424 _tribe_wooticket_event 263 2660 424 _tribe_wooticket_order 416 2536 416 3 Attendee Name Bob Squrepants 2657 423 _tribe_wooticket_event 263 2656 423 _tribe_wooticket_order 416 2530 416 1 Attendee Name John Smith 2657 423 _tribe_wooticket_event 263 2656 423 _tribe_wooticket_order 416 2533 416 2 Attendee Name Fred Bloggs 2657 423 _tribe_wooticket_event 263 2656 423 _tribe_wooticket_order 416 2536 416 3 Attendee Name Bob Squrepants 2653 422 _tribe_wooticket_event 263 2652 422 _tribe_wooticket_order 416 2530 416 1 Attendee Name John Smith 2653 422 _tribe_wooticket_event 263 2652 422 _tribe_wooticket_order 416 2533 416 2 Attendee Name Fred Bloggs 2653 422 _tribe_wooticket_event 263 2652 422 _tribe_wooticket_order 416 2536 416 3 Attendee Name Bob Squrepants 2646 421 _tribe_wooticket_event 263 2645 421 _tribe_wooticket_order 420 2636 420 1 Attendee Name John Smith 2595 419 _tribe_wooticket_event 263 2594 419 _tribe_wooticket_order 417 2578 417 1 Attendee Name Chris B Bacon 2595 419 _tribe_wooticket_event 263 2594 419 _tribe_wooticket_order 417 2581 417 2 Attendee Name AN Other 2591 418 _tribe_wooticket_event 263 2590 418 _tribe_wooticket_order 417 2578 417 1 Attendee Name Chris B Bacon 2591 418 _tribe_wooticket_event 263 2590 418 _tribe_wooticket_order 417 2581 417 2 Attendee Name AN Other

 

Please let me know if you need anything else

 

export-BXzfCZN3.gif - http://picpaste.com/export-BXzfCZN3.gif

Edited by jarvis
Link to comment
Share on other sites

  • Solution

I've changed the join condition for pm2

SELECT DISTINCT
    pm1.meta_value AS 'ORDER No'
    , pm1.post_id AS 'TICKET No'
    , pm2.meta_value AS 'Guest Name'
FROM $wpdb->postmeta pm
INNER JOIN $wpdb->postmeta pm1
    ON pm.post_id = pm1.post_id AND pm1.meta_key = '_tribe_wooticket_order'
INNER JOIN $wpdb->postmeta pm2
    ON pm1.meta_value = pm2.post_id AND pm2.meta_key LIKE '% Attendee Name'
WHERE pm.meta_key = '_tribe_wooticket_event'
AND pm.meta_value = 263;
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.