本文介绍了使 mySQL SUBSTRING 工作的小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我对此非常接近,因为我之前收到了有关此主题的一些帮助,但是当它使相同的代码适用于网站的另一部分时,似乎我无法再次使用它.

I know I am pretty close with this, as I have recieved some help on this topic earlier, but when it cam to making the same code work for another part of the site it seems I cannot get it working again.

我正在做的是尝试获取字符串的第一部分(由数据库中的 - 分隔符破坏),因此数据库条目 slug 将是foo-bar-rules,我需要从 slug 不等于 foo-bar-rulesfoo/code> 但等于 pho-bar-rulesfeuw-bar-rules.很抱歉,如果我的解释不是很有帮助,我无法向自己解释:D.

What I am doing is trying to get the first part of a string (which is broken by a - delimiter in the database) so the db entry slug would be foo-bar-rules, I need to grab foo from that string where slug is not equal to foo-bar-rules but equal to phoo-bar-rules or feuw-bar-rules. Am sorry if my explanation was'nt very helpful, I had trouble explaning it to myself :D.

提前感谢任何可以帮助我的人..

Thank you in advance anyone that can help me out here..

<?php define ('PAGEPARENT', 'foo');
      define ('PAGECHILD', 'bar');
      define ('PAGEGRANDCHILD', 'rules');

    switch (PAGEGRANDCHILD) {
        case PAGEGRANDCHILD:
            $pageGrandChild = PAGEGRANDCHILD;
            $rangeRelationResult = mysql_query("SELECT DISTINCT SUBSTRING(slug, 1, INSTR(slug, '-') - 1) result
                                                FROM web_navbar_links
                                                WHERE SUBSTRING(slug FROM INSTR(slug, '-".PAGECHILD."-') + 1) = '$pageGrandChild'
                                                AND grandchild = 1
                                                AND slug != '".PAGEPARENT."-".PAGECHILD."-".PAGEGRANDCHILD."';
                                                ");
            while ($rangeRelationRow = mysql_fetch_object($rangeRelationResult)) { ?>
                <a href="?page=<?php echo $rangeRelationRow->result."-".PAGECHILD."-".PAGEGRANDCHILD; ?>&pageLevel=<?php echo $_GET['pageLevel']; ?>" title="<?php echo PAGEGRANDCHILD."&nbsp;for&nbsp;".$rangeRelationRow->result; ?>"><div id="<?php echo $rangeRelationRow->result; ?>Channel"><?php echo "&raquo;&nbsp;".ucwords(PAGECHILD)." for ".$rangeRelationRow->result; ?></div></a> <?php
            }
        break;
    }  ?>

推荐答案

我不完全确定我的问题是否正确,但是 LIKE 是一个选项吗?

I'm not entirely sure whether I get your question right, but is LIKE an option?

SELECT .... WHERE slug LIKE "%-rules" AND slug != "foo-bar-rules" 

将选择其 slug 以 -rules 结尾的所有记录.

would select all records whose slug ends in -rules.

这篇关于使 mySQL SUBSTRING 工作的小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:20