如何通过左连接链接视图订购类别?

时间:2018-01-10 08:39:48

标签: php mysql select join sum

我希望有一个列表,其中包含按另一个表格中的总链接视图排序的类别。

我只是尝试google一点,然后想出了这个例子。

<?php
include 'config.php';
$db = new DB();
$sql="SELECT categories.name,
    (
        SELECT SUM(hits)
        FROM links
        WHERE catId = categories.catId
    ) AS hits_count
FROM categories
LEFT JOIN links
ON links.catId = categories.catId
WHERE links.status=1
ORDER BY hits_count DESC";

$query = $db->db->prepare($sql);
$categories = $query->execute();
?>
<?php print_r($categories) ?>

这会有用吗?或者还有另一种方法吗?

最新的尝试

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.s2s.doupnow, PID: 26707
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.s2s.doupnow/com.s2s.doupnow.VideoPlayActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class TextView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.s2s.doupnow.VideoDescriptionFragment.onCreateView(VideoDescriptionFragment.java:16)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
at android.app.Activity.onCreateView(Activity.java:4797)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:692)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.s2s.doupnow.VideoPlayActivity.onCreate(VideoPlayActivity.java:61)
at android.app.Activity.performCreate(Activity.java:5242)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249) 
at android.app.ActivityThread.access$800(ActivityThread.java:141) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5113) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 

只返回数字1

1 个答案:

答案 0 :(得分:0)

我会将此短语称为categories与查找总和的子查询之间的连接:

SELECT
    c.catId,
    c.title,
    c.description,
    COALESCE(l.hits_count, 0) AS hits_count
FROM categories c
LEFT JOIN
(
    SELECT catId, SUM(hits) hits_count
    FROM links
    GROUP BY catId
) l
    ON l.catId = c.catId
WHERE
    c.status = 1
ORDER BY
    hits_count;

通过使用连接而不是相关子查询,您应该获得性能提升。但也许你遇到的更大的问题是你在所有东西周围加上单引号。不要这样做,因为MySQL可能会将其解释为字符串文字。如果您需要在MySQL中转义表名或列名,请使用反引号。但在你的情况下,我认为你根本不需要它们。