Mysql select Query with multiple conditions of same column - mysql

I need MySQL select query like
select wt_id,wt_name from work_type where cat_id=1,2,5..;
Is it possible?

Use IN operator ex.
... where cat_id IN (1,2,5..)

You just have to use IN operator
SELECT wt_id, wt_name
FROM work_type
WHERE cat_id IN (1,2,5..);

You can use SQL IN operator.
SELECT wt_id, wt_name
FROM work_type
WHERE cat_id IN (1,2,5..);
If you have any question please comment below.

Related

Is there any way in MySQL, to create a single select statement where the select column is the result of another query?

My first select statement would be like below,
SELECT m.col_name
, m.col_alias
FROM <table_name> m
WHERE m.exportable LIKE '%Y%'
And I'm trying to create a second select query with the data that I'm receiving from the first statement, like below
SELECT tabella.id alias_1
, tabella.value alias_2
, **“list of col_name result of the previous query”
FROM <another_table> tabella
WHERE tabella.metadata_id = 'CI_INDEX||CI_01'*
Thanks in advance.
Try the Subquery.
You will find an example in the links.
If the tables have relation between them then go for Join
if the tables do not have relation you can use the following format.
select table1.col1,table2.col1 from table1, table2
where table1.colx like '%exp%' and table2.coly like '%exp2%'

distinct multiple columns in SQL

I want to select distinct combination of user age (a column name) and user name (another column name) , but when I write distinct (user_age, user_name), there is syntax error. If anyone have ideas how to write distinct with multiple columns it will be great.
BTW, using MySQL Workbench/MySQL
thanks in advance,
Lin
You have to leave the parenthesis. Just write
SELECT distinct user_age, user_name FROM foo where bar;
;-)
There are 2 ways to achieve this
SELECT DISTINCT user_age, user_name FROM table WHERE some_condition;
OR
SELECT user_age, user_name FROM table WHERE some_condition GROUP BY user_age, user_name;
Hope this helps

Need help for mysql query

In my MySQL table I have a column named member_id. That column stores values like this:
1,2,3,4,5
I need to check that by using
SELECT *
FROM member
WHERE 5 IN member_id
It's not working well. Please help me write a SQL query that will find the appropriate results.
Use this query:
SELECT *
FROM mytable
WHERE FIND_IN_SET(5, member_id)
Quassnoi's method is preferred for MySQL, but for a portable cross-platform technique, you can do this:
select *
from member
where member_id = '5'
or member_id like '5,%'
or member_id like '%,5,%'
or member_id like '%,5'
This assumes there is no extra whitespace in your data.
SELECT * FROM member WHERE member_id LIKE '%5%'

Can we use aliased field to use in ORDER BY clause in MySQL?

Can we use aliased field name in ORDER BY clause?
For example:
SELECT id, name AS firstname
FROM users
ORDER BY firstname
Is it possible? When I tried this it errored out.
An alias can be used in a query select list to give a column a
different name. You can use the alias in GROUP BY, ORDER BY, or HAVING
clauses to refer to the column.
Check here.
Try with this one, I have added back-ticks to column name and table Because name is the reserved word :
SELECT `id`, `name` AS firstname
FROM `users`
ORDER BY firstname
Yes, you can certainly use column aliases in your "order by" clause.
You can verify it works with the built-in mySql "user" table:
select User as name,Host from user order by name;
If it "errored out", then something else must have been wrong with your query. Please cut/paste the exact error message.
You cannot use alias in GROUP BY Clause in MS SQL SERVER
In order to tell of you can use aliases in certain clauses, check the order in which SQL procceses the the queries and it is as follows:
-from
-where
-group by
-having
-order by

sql columns adding

I am trying to add two columns as one column by using this statement
SELECT (member_Firstname+''+member_Lastname) AS Name FROM members
but it gives all 0 values in mysql workbench
SELECT concat(member_Firstname,'',member_Lastname) AS Name FROM members;
This should work always
I think that in MySQL you should use CONCAT, as follows:
mysql> SELECT CONCAT('My', 'S', 'QL'); -> 'MySQL'
Adding is for numbers; for joining strings, use concat()
SELECT CONCAT(string1,string2,string3,etc) FROM table
SELECT CONCAT(CAST(int_col AS CHAR), char_col);
CONCAT() returns NULL if any argument is NULL.
So for the example
SELECT CONCAT(member_Firstname, member_Lastname);