How to improve this select statement [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I wrote this SQL
select *
from t_info
where contract_id <> "" and status <> "unfinished"
order by id asc;
but it takes more than 4s. I wonder how to improve this SQL? Thank.

Please don't use "<>" in where condition, while you need to use index.
select * from t_info
where LENGTH(trim(contract_id)) > 0
and (not status = "unfinished")
order by id asc;

Related

Is it possible to optimize this where clause? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Can anyone help me with this? I have doubts about the below function; can I create a virtual column for this?
select as1.col,as1.col2,as1.col3 from
analytics.adjusted_sale_velocity
where
date(as1.created_datetime)=(
select
max(
date(created_datetime)
)
from
analytics.adjusted_sale_velocity
)
MySQL optimizer won't use an index once a column in the WHERE clause is wrapped with a function, date in your case.
Your query might be written a little different:
select as1.col,
as1.col2,
as1.col3
from adjusted_sale_velocity a
inner join ( select max(created_datetime) as created_datetime
from adjusted_sale_velocity
) as max_dt on left(a.created_datetime,10) = left(max_dt.created_datetime,10) ;
Try and let me know if it is faster.

I want data from datatable and replace comma with space [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have query for to get data following :
select a from REPLACE(M,',','')TABLE;
I want to get all the data from table along with M column content replce , with space.
You select from the table. What you select belongs in the SELECT clause:
SELECT t.*, REPLACE(m, ',', '') FROM mytable t;

Duplicate value while using Group By [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
SELECT location,special,price FROM `tickets`
WHERE event = 'food' GROUP BY location
I think you want, (assuming its mysql) the following:
SELECT location, ANY_VALUE(special) special, ANY_VALUE(price) price
FROM tickets
WHERE event='food'
GROUP BY location ;
ANY_VALUE is strange aggregate function out of normal db standards that returns one of the values arbitarily.
If you wish to not have duplicate results in your rows you can choose only distinct result set.
SELECT DISTINCT location,special,price FROM tickets WHERE event = 'food' GROUP BY location

MySQL - Featch Records with limit in one table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have one table with 3 fields e.g.
Name, Team Name, Player_Number
there is multiple team and I want to fetch 2 member from each team.
Please share with fast solution.
Try this
select * from TableName s where (select count(*) from TableName a where a.TeamName = s.TeamName and a.Player_Number >= s.Player_Number) <= 2

String retrieve Function in SQL Query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am having a database with 2 columns employee and job.Job column contains character in each record.In that,I have to list the employees whose job is having last 3 characters as 'man'.I have to use only functions in SQL Query.
Use LIKE with wildcard:
SELECT *
FROM your_table
WHERE job_column_name LIKE '%man'
or using RIGHT
SELECT *
FROM your_table
WHERE RIGHT(job_column_name, 3) = 'man'
Answer For Question 4 is
SELECT *
FROM emp
WHERE SUBSTR(job,-3,3)='MAN';