简体   繁体   English

sql查询无法使用order by

[英]sql query not working with order by

Here is my original query that works 这是我的原始查询有效

 Select * FROM story st, sentences s, speaker sp 
 WHERE (st.lesson_id = '1' AND 
        st.speaker_id = sp.speaker_id AND 
        st.sentence_id = s.sentence_id)

When I try to add a Order By it breaks down. 当我尝试添加订单时,它会崩溃。

 Select * FROM story st, sentences s, speaker sp 
 WHERE (st.lesson_id = '1' AND 
        st.speaker_id = sp.speaker_id AND 
        st.sentence_id = s.sentence_id) ORDER BY st.story_in_lesson_id ASC

Can't figure out why it is breaking. 无法弄清楚它为什么会破碎。

EDIT: Here is the error I get Fatal error: Call to a member function fetch_object() 编辑:这是我得到的错误致命错误:调用成员函数fetch_object()

EDIT: My PHP code 编辑:我的PHP代码

$result = $mysqli->query("Select * FROM story st, sentences s, speaker sp 
                         WHERE st.lesson_id = '1' AND 
                         st.speaker_id = sp.speaker_id AND 
                         st.sentence_id = s.sentence_id 
                         ORDER BY st.story_in_lesson_id ASC");

while ($value = $result->fetch_object()) { 
    //never goes in here fails at the fetch_object()
}

EDIT: 编辑:

Is it possible that it does not work because I am trying to query multiple tables? 是否有可能它不起作用,因为我试图查询多个表? Only one of the tables has the story_in_lesson_id which is the story table. 只有一个表具有story_in_lesson_id,即故事表。 When I run the query just on that table it works find. 当我在该表上运行查询时,它可以找到。

EDIT: 编辑:

More info, copied the DB over to my work Mac and the query works!!! 更多信息,将数据库复制到我的工作Mac并查询工作! But why does it not work on my computer?? 但为什么它不能在我的电脑上工作? By the way I am strictly testing the query now by just running it inside Sequel Pro. 顺便说一下,我现在通过在Sequel Pro中运行它来严格测试查询。

Please remove your where condition dear ..... may be it's working and please give me reply what you getting result . 请删除你的地方亲爱的.....可能它正在工作,请给我回复你得到的结果。

SELECT * 
FROM   story st 
       INNER JOIN sentences s 
               ON st.sentence_id = s.sentence_id 
       INNER JOIN speaker sp 
               ON st.speaker_id = sp.speaker_id 
ORDER  BY st.story_in_lesson_id ASC 

You have to check st.story_in_lesson_id exists in your story table and to prevent from Fatal error: Call to a member function fetch_object() you should try it like, 您必须检查story table是否存在st.story_in_lesson_id并防止Fatal error: Call to a member function fetch_object()您应该尝试它,

$query = "Select * FROM story st, sentences s, speaker sp 
              WHERE st.lesson_id = '1' AND 
              st.speaker_id = sp.speaker_id AND 
              st.sentence_id = s.sentence_id 
              ORDER BY st.story_in_lesson_id ASC";
              // check story_in_lesson_id exists in story table 

if ($result = $mysqli->query($query)) {
    while ($value = $result->fetch_object()) { 
       // while code
    } // while ends
} // if result ends

Try this: 试试这个:

SELECT * 
FROM story st
INNER JOIN sentences s ON st.sentence_id = s.sentence_id
INNER JOIN speaker sp ON st.speaker_id = sp.speaker_id
WHERE st.lesson_id = '1' 
ORDER BY st.story_in_lesson_id ASC

Thank you for all the help! 谢谢你的帮助! I have solved the issue!!! 我已经解决了这个问题!

In the end it ended up being a file writing permissions issue. 最后它最终成为文件写入权限问题。 I figured it out by running my query in Sequel Pro which threw a error about some file located at /var/folders/41/some big number/T/ . 我通过在Sequel Pro中运行我的查询来解决这个问题,该问题引发了一个关于/ var / folders / 41 /某些大数字/ T /的文件的错误。 I went to the dir and allowed for write access tried the query in Sequel Pro and it worked. 我去了dir并且允许写入访问尝试了Sequel Pro中的查询并且它有效。 Then I go to my php and try it there and it worked. 然后我去我的PHP并尝试它,它的工作原理。

If anyone can answer why this happened? 如果有人能回答为什么会这样? Do all queries write to a log file or anything like that? 是否所有查询都写入日志文件或类似的内容? If anyone can share some more inside. 如果有人可以在里面分享更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM