MySQL multi-expression if statement
P粉748218846
P粉748218846 2023-09-19 16:06:52
0
1
541

I want to execute multiple expressions in an if statement within a select statement like this:

SELECT 
  t.id,
  IF (
    id > 10,
    @var1 := t.id; @var2 := t.title,
    t.title
  )
FROM
  table

Is there a way to execute these two expressions @var1 := t.id; @var2 := t.title in one if statement?

P粉748218846
P粉748218846

reply all(1)
P粉674876385

You can do this

SELECT
  t.id,
  IF (
    id > 10,
    CONCAT(@var1 := t.id , @var2 := t.title),

    t.title
  )
FROM
  table1 t;
SELECT @var2;
SELECT @var1;

But user-defined variables are scalar values, so you can only get the last value of @var1 and @var2 that were selected, but it will not display the connected value now.

So overall, this doesn't really make sense if you have multiple rows of data.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!