IF语句MYSQL中的SUBSTR()函数

时间:2017-07-19 08:36:18

标签: mysql sql if-statement substring

查询:

<Window.Resources>
    <DataTemplate x:Key="lbxHeaderDataTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1.5*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Label Content="{Binding Item1}"></Label>
            <ComboBox Name="cbxTest" Grid.Column="1" ItemsSource="{Binding 
                 Item2}" DisplayMemberPath="Key"></ComboBox>
        </Grid>
    </DataTemplate>

</Window.Resources>
<StackPanel Width="auto" Height="auto">
    <ListBox Name="lbxFields"
             ItemTemplate="{DynamicResource lbxHeaderDataTemplate}" 
             HorizontalContentAlignment="Stretch">
    </ListBox>
</StackPanel>

在上面的查询中,当表达式为private List<KeyValuePair<string, int>> cbxOptions2 = new List<KeyValuePair<string, int>>(); cbxOptions2.Add(new KeyValuePair<string, int>("", 0)); cbxOptions2.Add(new KeyValuePair<string, int>("Identifier", 0)); cbxOptions2.Add(new KeyValuePair<string, int>("Family Identifier", 0)); cbxOptions2.Add(new KeyValuePair<string, int>("File Path", 0)); for (int i = 0; i < 10; i++) { lbxDatFields.Items.Add(new Tuple<string, List<KeyValuePair<string, int>>>((i * 10).ToString(), cbxOptions2)); } 时,我无法使用SELECT if(substr(link, 1, 2)='y_', 'https://www.youtube.com/watch?v='substr(link,3), 'https://vimeo.com/'substr(link,3)) as link, `title` FROM `video` WHERE `reg_id` = '101' 函数。在substr(link,3)之后,我想在表格中获得true列的子字符串值。

以上查询引发了一些错误。所以请帮我解决这个问题。答案将不胜感激。

3 个答案:

答案 0 :(得分:2)

在mysql中,如果要连接字符串,则需要使用concat()函数:

SELECT if(substr(link, 1, 2)='y_', concat('https://www.youtube.com/watch?v=',substr(link,3)), concat('https://vimeo.com/',substr(link,3))) as link, `title`
FROM `video`
WHERE `reg_id` = '101'  

答案 1 :(得分:1)

您不能通过一个接一个地编写字符串来梳理字符串。您必须使用CONCAT功能。

public static void LoadSettings(string fileName)
{
    try
    {
        var xml = new XmlDocument();
        xml.Load(fileName);
        var userNodes = xml.SelectNodes("/settings");

        foreach (XmlNode node in userNodes)
        {
            globals.username = node.SelectSingleNode("username").InnerText;
            globals.password = node.SelectSingleNode("password").InnerText;
            globals.rank = node.SelectSingleNode("rank").InnerText;
        }
    }
    catch
    {
        Console.WriteLine("Oops, something is wrong.");
    }
}

答案 2 :(得分:1)

您可以使用CONCAT()功能将两个字符串添加到一起:

SELECT if(substr(link, 1, 2)='y_', CONCAT('https://www.youtube.com/watch?v=',substr(link,3)), CONCAT('https://vimeo.com/',substr(link,3))) as link, `title`
FROM `video`
WHERE `reg_id` = '101'