从三个wpdb表获取记录

时间:2016-10-27 作者:Rafal

我试图处理wpdb,但不幸的是,我正在创建的东西甚至还没有接近我所需要的。我有数据:

卖家ID(post\\u author=user\\u ID)-$卖家ID

(post\\u parent=transaction\\u id)-$ordernum

我有三张桌子:

wp_dopbsp_calendars

id
user_id
post_id name
max_year
hours_enabled
hours_interval_enabled
price_min
price_max 
rating 
address address_en
address_alt
address_alt_en
coordinates

wp_dopbsp_reservations

id
calendar_id
language
currency
currency_code
check_in
check_out
start_hour
end_hour
no_items
price
price_total
refund
extras
extras_price
discount
discount_price
coupon
coupon_price
fees
fees_price
deposit
deposit_price
days_hours_history
form
address_billing
address_shipping
email
status
payment_method
payment_status
transaction_id
token
date_created
以及wp_posts

ID
post_author
post_date
post_date_gmt
post_content
post_title
post_excerpt
post_status
comment_status
ping_status
post_password
post_name
to_ping
pinged
post_modified
post_modified_gmt
post_content_filtered
post_parent
guid
menu_order
post_type
post_mime_type
comment_count
我需要创建表并获取其唯一记录:

按卖家ID(post\\u author=user\\u ID)获取日历ID(“ID”表wp\\u dopbsp\\u日历)和post\\u父级(“post\\u父级”来自wp\\u posts)

用这两个查找表中的行wp_dopbsp_reservations, 从那里出发只有那些transaction_idpost_parent (来自wp\\U岗位)和calendar_idid (来自wp_dopbsp_calendars)

此表中需要的列包括transaction_id, calendar_id, check_in, check_out, start_hour, end_hourprice_total.

我只能这样说:

Select
  $wpdb->posts.post_author,
  $wpdb->dopbsp_calendars.user_id,
  $wpdb->dopbsp_reservations.price_total,
 $wpdb->dopbsp_reservations.check_in,
  $wpdb->dopbsp_reservations.check_out,
  $wpdb->dopbsp_reservations.start_hour,
  $wpdb->dopbsp_reservations.end_hour,
  $wpdb->dopbsp_calendars.id,
  $wpdb->posts.post_title,
  $wpdb->posts.ID As ID1,
  $wpdb->posts.post_parent,
  $wpdb->dopbsp_reservations.transaction_id
From
  wp_posts
  INNER JOIN $wpdb->posts.post_author ON $wpdb->dopbsp_calendars.user_id
  $wpdb->dopbsp_calendars
    On wp_posts.post_author = $wpdb->dopbsp_calendars.user_id,
  $wpdb->dopbsp_reservations

1 个回复
SO网友:Max Yudin

你不想问wpdb 对于每个字段。这是SQL查询的包装器

类似(未测试):

<?php
$my_results = $wpdb->get_results(
    SELECT
        posts.post_author, dopbsp_calendars.user_id, dopbsp_reservations.price_total, dopbsp_reservations.check_in, dopbsp_reservations.check_out, dopbsp_reservations.start_hour, dopbsp_reservations.end_hour, dopbsp_calendars.id, posts.post_title, posts.ID As ID1, posts.post_parent, dopbsp_reservations.transaction_id
    FROM wp_posts
    INNER JOIN
        posts.post_author
    ON
        dopbsp_calendars.user_id
        dopbsp_calendars
    ON
        wp_posts.post_author = dopbsp_calendars.user_id,
        dopbsp_reservations
);
然后查看此对象中的内容:

<?php
echo \'<pre>\';
print_r($my_results);
echo \'</pre>\';
警告:不应直接调用wpdb()类中的方法。改用全局$wpdb对象!Class Reference/wpdb

所以,最好$wpdb 全球:

global $wpdb;
$my_results = $wpdb->get_results(..................);
注意:我没有检查查询本身。

相关推荐

$wpdb->get_row返回什么?

由于插件中的调试和错误查找,我有一个简短的问题。$wpdb->;如果没有结果,返回get\\u row()?文件说明:(array|object|null|void) Database query result in format specified by $output or null on failure. 但当查看源代码时,它似乎返回了;null“;。。。如果您能快速提供帮助,我将不胜感激,我一直认为它会返回一个空数组,但我可能弄错了!