I got a question. I will try to describe my situation:

我有一个问题。我会尽量描述我的情况:

I got a wordpress installation which i installed codeigniter in. This all goes good and i also got access to the database. Now i have a plugin installed for wordpress called: Woocommerce. With this plugin you can store products and productdata in your database. Now i need to access the data from my woocommerce products within the codeigniter application.

我安装了wordpress,安装了codeigniter。这一切都很好,我也可以访问数据库。现在我为wordpress安装了一个插件:Woocommerce。使用这个插件,您可以在数据库中存储产品和产品数据。现在我需要在codeigniter应用程序中访问来自我的woocommerce产品的数据。

Woocommerce stores its products like this:

Woocommerce商店的产品是这样的:

All products goes into a table called: wp_posts In this table there is a column defined called: post_type Woocommerce identifies the posts as products when post_type is set to product [duh!].

所有产品都进入一个名为:wp_posts的表中有一个列被定义为:post_type Woocommerce,当post_type被设置为product时将这些post标识为产品[duh!]。

Now there is another table called wp_postmeta. In this table all of the productdata get stored into 4 columns: 1. meta_id [identifier for the metarow] 2. post_id [identifies itself to the wp_posts table] 3. meta_key [there will go several keys into it like: sale_price, stock, additional_price etc..] 4. meta_value [for each key there is a value.]

现在有另一个表叫做wp_postmeta。在这个表中,所有的productdata都存储在4列中:1。meta_id [metarow的标识符]2。post_id[将自己标识为wp_posts表]3。meta_key[会有几个键,比如:sale_price、stock、additional_price等。)4。meta_value[每个键都有一个值]。

In the table wp_postmeta i need to sort out all meta_keys with their values if the product got a meta_key='_rentable' and the meta_value='yes'. So if this is true, i need to get all the other meta_keys and values where the post_id is the same as the rentable product. I hope i didn't confuse anyone... For now i got this query:

在表wp_postmeta中,如果产品获得了meta_key='_rentable'和meta_value='yes',那么我需要用它们的值对所有的meta_keys进行排序。因此,如果这是真的,我需要获得所有其他的meta_keys和值,其中post_id与可出租产品相同。我希望我没有迷惑任何人……现在我得到了这个问题:

    $sql = "SELECT p.id, p.post_title, p.guid, p.post_type, m.meta_key, m.meta_value
            FROM wp_posts p
            INNER JOIN wp_postmeta m
            WHERE p.id=m.post_id
            AND m.meta_key='_rentable' AND m.meta_value='yes'
            ";

This only returns the meta_key: _rentable and the value: yes.. But i also need to get the price of that product.

这只返回meta_key: _rentable和值:yes..但是我还需要知道那个产品的价格。

1 个解决方案

#1


10

It sounds like you're trying to get several pieces of data from different rows in the same table, which means you need to do more than one join. Does this get you what you're looking for?

听起来好像您试图从同一个表中的不同行获取多个数据,这意味着您需要执行多个连接。这能得到你想要的吗?

$sql = "SELECT p.id, p.post_title, p.guid, p.post_type, m.meta_key, m.meta_value, meta_sp.meta_value as sale_price, meta_ap.meta_value as additional_price
        FROM wp_posts p
        INNER JOIN wp_postmeta m
        INNER JOIN wp_postmeta meta_sp ON p.id=meta_sp.post_id 
            AND meta_sp.meta_key='sale_price'
        INNER JOIN wp_postmeta meta_ap ON p.id=meta_ap.post_id 
            AND meta_ap.meta_key='additional_price'
        WHERE p.id=m.post_id
        AND m.meta_key='_rentable' AND m.meta_value='yes'
        ";

更多相关文章

  1. Spring Security ACL使用MySQL配置与数据库脚本
  2. 数据库行转列和列转行小例子
  3. 加载数据INFILE不工作。
  4. MySQL数据库储存bit类型的值报错
  5. 多个mysql数据库怎么指定到多个不同文件目录
  6. 向数据库添加1的PHP Onclick事件(SQL)
  7. 一步一步学MySQL----9 条件数据记录查询
  8. navicat for mysql 传输数据失败
  9. MySQL数据库以及相关知识点总结大全

随机推荐

  1. 〖Linux〗穿越城墙之后,直接连接国内网站
  2. Android(安卓)WebView显示HTML时右边有空
  3. android酷炫翻页效果+图形分析
  4. Android(安卓)数据存储---File内部存储
  5. Android7.0中文API---ImageButton
  6. 如何检索Android设备的唯一ID
  7. Android 学习资源链接
  8. Android设置全屏
  9. Android NDK开发(一)HelloWorld
  10. 王家林的81门一站式云计算分布式大数据&