I feel like I've done this before but missing something. I'm going through a database I inherited and want to see a bunch of DISTINCT values. I was thinking I could do something like this instead of writing cf_840 (or whatever number) a bunch of times and just change the actual field name in one spot...
SET #var = 'cf_840';
SELECT DISTINCT #var, COUNT(#var) AS counter
FROM vtiger_leadscf
GROUP BY #var
ORDER BY #var;
But this isn't working right and I feel like I'm missing something simple but can't find the right thing to search on SO.
You cannot use variables to directly specify fields outside of dynamically constructing a query; at best you can choose a value from a field conditionally, like CASE #var WHEN 840 THEN cf_840 WHEN 1 THEN cf_1 .... etc END AS fieldVal
Otherwise, you need to dynamically construct a query string with the field name "baked" into the query that gets executed.
C# style: var query = String.Format("SELECT {0}, COUNT(DISTINCT {0}) AS counter FROM .... blah blah blah", fieldName);
SQL Proc Style: SET query := 'SELECT ' + fieldName + ', COUNT(DISTINCT ' + fieldName.....and so on, then PREPARE and EXECUTE.
Edit: I'm not sure why you're selecting #var if you're getting the count of its values. I am guessing you want the field name included in the result, so perhaps the examples would be better as SELECT '{0}' as theField, COUNT(DISTINCT {0}) AS counter ... and 'SELECT ''' + fieldName + ''' AS fieldName, COUNT(DISTINCT ' + fieldName ....`
Also, you should not need GROUP BY or ORDER BY clauses, these queries will have only one result row.
To use a variable in the current query, declare it in the body of the SELECT
SELECT DISTINCT #var := 'cf_840' AS fieldName, COUNT(#var) AS counter
FROM vtiger_leadscf
GROUP BY fieldName
ORDER BY counter;
Related
How i can get value like this in a variable 'TFSEP-2019','TFjul-2018','TFJun-2018' without spaces.
SELECT s.house, s.grade, s.homeroom AS Campus
FROM student s,fees_jrl f
WHERE s.studnum = f.studnum AND
f.name IN (' TFJun-2018 TFJul-2018 ') AND
f.trans_type= 'chg' AND
f.paid_id is NULL AND s.house LIKE '%'
GROUP BY s.house
I am getting like this ('TFJun-2018 TFJUL-2018 TFSEP-2019') but I want like ('TFSEP-2019','TFjul-2018','TFJun-2018') please help
You may refer the following statements with like operators, whenever you need quotes('), use doubled-quotes('') :
create table tab ( Id int, expression varchar(100));
insert into tab values(1 ,'TFJun-2018 TFJUL-2018 TFSEP-2019');
insert into tab values(2,'''TFJun-2018'',''TFJUL-2018'',''TFSEP-2019''');
select * from tab where expression like '''TFJun-2018''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
or
select * from tab where expression like '''TFJun-2018'',''TFJUL-2018''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
or
select * from tab where expression like '''TFJun-2018'',''TFJUL-2018'',''TFSEP-2019''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
Rextester Demo
You can use like for strings
% means any chars, _ means one char
So doing
F.name like %2018%
Will give you all of 2018..
use explicit join and concat
SELECT
concat( concat( concat( concat("'",s.house),"'"),concat( concat("'",s.grade),"'")),
concat( concat("'",s.homeroom),"'"))
FROM student s join fees_jrl f
on s.studnum = f.studnum
where f.name IN (' TFJun-2018 TFJul-2018 ') AND
f.trans_type= 'chg' AND
f.paid_id is NULL AND s.house LIKE '%'
I think you dont need group by as you have no aggregation function
I'm looking to take a specified string and query a table where a concat of 2 fields is equal to the string.
set #fab = "36013-601301-11";
set #job = substring_index(#fab, '-', 1);
set #fabnumba = trim(leading LEFT(#fab,char_length(#job)+1) from #fab);
select * from (select JobNumber, concat(JobNumber, '-', LotNumber) as bomfab from qiw_powerbi) base
where bomfab LIKE concat(#job,"-", #fabnumba)
If I try the following it fails:
WHERE bombfab LIKE "36013-601301-11"
However, this attempt works:
WHERE bombfab LIKE "36013-%601301-11"
How can I concat() with the variables #job and #fabnumba to do this?
Are you sure that the LotNumber values from qiw_powerbi are what you are expecting? They don't have any leading spaces?
What happens if you try adding a TRIM function to LotNumber:
select * from (select JobNumber, concat(JobNumber, '-', TRIM(LotNumber)) as bomfab from qiw_powerbi) base
where bomfab LIKE concat(#job,"-", #fabnumba)
So, I would like to improve a shoutbox plugin for MyBB and do the following in pMA.
Private shouts are stored as TEXT/VARCHAR: "/pvt userid message". What I want to achieve is to:
move value in userid to the new INT column touid.
remove the /pvt id prefix
Earlier the code used sscanf($message, "/pvt %d", $userID).
The query would be something like UPDATE shouts SET touid=???, message=??? WHERE message LIKE '/pvt %'.
#edit: Examples:
/pvt 55 Hello world - this is a private shout!
/pvt 675 Another pm.
This is not a private shout
The first part is not so hard:
update shouts
set touid = substring_index(message, '/pvt ', -1) + 0
where message LIKE '/pvt %';
This splits the string on '/pvt ', takes the second part and converts it to an integer. MySQL does silent conversion, so it converts the leading number.
An alternative takes advantage of the fact that the pattern is at the beginning:
update shouts
set touid = substr(message, 6, 1000) + 0
where message LIKE '/pvt %';
You can get rid of this part of the string by replacing it with nothing:
update shouts
set touid = substring_index(message, '/pvt ', -1) + 0
message = replace(message,
concat('/pvt ', substring_index(message, '/pvt ', -1) + 0),
'')
where message LIKE '/pvt %';
I have a field urn_sem.studentid that I'd like to replace a few characters in; for example:
ABC/2011/BCOMH_NC/I/12 → ABC/2011/BCOMH/I/12
ABC/2011/BCOMH_NC/I/24 → ABC/2011/BCOMH/I/24
I've tried this query:
SELECT REPLACE(studentid, 'KNC/2011/BCOMH_NC/', ' KNC/2011/BCOMH/')
FROM urn_sem
but it doesn't show the new value.
Do you want this:
update urn_sem
set studentid = REPLACE(studentid, 'KNC/2011/BCOMH_NC/', ' KNC/2011/BCOMH/')
where studentid like '%KNC/2011/BCOMH_NC/%'
The WHERE clause is optional. It ensures that the replace is only on rows that change.
And this sample query does not work?
SELECT REPLACE (studentid, '_', '') FROM urn_sem
I'm don't have a lot of knowledge of MySql (or SQL in general) so sorry for the noobness.
I'm trying to update a bunch of String entries this way:
Lets say we have this:
commands.firm.pm.Stuff
Well I want to convert that into:
commands.firm.pm.print.Stuff
Meaning, Add the .print after pm, before "Stuff" (where Stuff can be any Alphanumerical String).
How would I do this with a MySql Query? I'm sure REGEXP has to be used, but I'm not sure how to go about it.
Thanks
Try something like this. It finds the last period and inserts your string there:
select insert(s, length(s) - instr(reverse(s), '.') + 1, 0, '.print')
from (
select 'commands.firm.pm.Stuff' as s
) a
To update:
update MyTable
set MyColumn = insert(MyColumn, length(MyColumn) - instr(reverse(MyColumn), '.') + 1, 0, '.print')
where MyColumn like 'commands.firm.pm.%'
Perhaps use a str_replace to replace commands.firm.pm to commands.firm.pm.print
$original_str = "commands.firm.pm.15hhkl15k0fak1";
str_replace("commands.firm.pm", "commands.firm.pm.print", $original_str);
should output: commands.firm.pm.print.15hhkl15k0fak1
then update your table with the new value...How to do it all in one query (get column value and do the update), I do not know. All I can think of is you getting the column value in one query, doing the replacement above, and then updating the column with the new value in a second query.
To update rows that end in '.Stuff' only:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column) - CHAR_LENGTH('.Stuff') )
, '.print'
, '.Stuff'
)
WHERE Column LIKE '%.Stuff'
To update all rows - by appending .print just before the last dot .:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column)
- CHAR_LENGTH(SUBSTRING_INDEX(Column, '.', -1))
)
, 'print.'
, SUBSTRING_INDEX(Column, '.', -1)
)
WHERE Column LIKE '%.%'