String concatenation in MySQL

MySQL is different from most DBMSs’ use of + or || for concatenation. It uses the CONCAT function: SELECT CONCAT(first_name, ‘ ‘, last_name) AS Name FROM test.student There’s also the CONCAT_WS (Concatenate With Separator) function, which is a special form of CONCAT(): SELECT CONCAT_WS(‘ ‘, first_name, last_name) from test.student That said, if you want to … Read more

Merge two dataframes by index

Use merge, which is an inner join by default: pd.merge(df1, df2, left_index=True, right_index=True) Or join, which is a left join by default: df1.join(df2) Or concat), which is an outer join by default: pd.concat([df1, df2], axis=1) Samples: df1 = pd.DataFrame({‘a’:range(6), ‘b’:[5,3,6,9,2,4]}, index=list(‘abcdef’)) print (df1) a b a 0 5 b 1 3 c 2 6 d … Read more

Can I concatenate multiple MySQL rows into one field?

You can use GROUP_CONCAT: SELECT person_id, GROUP_CONCAT(hobbies SEPARATOR ‘, ‘) FROM peoples_hobbies GROUP BY person_id; As Ludwig stated in his comment, you can add the DISTINCT operator to avoid duplicates: SELECT person_id, GROUP_CONCAT(DISTINCT hobbies SEPARATOR ‘, ‘) FROM peoples_hobbies GROUP BY person_id; As Jan stated in their comment, you can also sort the values before … Read more

tech