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
Related
Here is my code:
SELECT COALESCE(CONCAT(u.user_fname, ' ', u.user_lname), 'unknown') name
FROM users u
WHERE id = 10;
The result will be unknown when either user_fname or user_lname is null. That's not what I want, I want to select unknown only when both user_fname and user_lname are null.
Otherwise, I want to get the value of not-null column. How can I do that?
Use concat_ws():
SELECT CONCAT_WS(' ', u.user_fname, u.user_lname) name
FROM users u
WHERE id = 10;
This has the nice benefit that if either of the names are null, you don't get a spurious space in the result.
It gets a little tricky if you want to convert NULLs to "unknown". This should do the trick:
SELECT COALESCE(NULLIF(CONCAT_WS(' ', u.user_fname, u.user_lname) , ''), 'unknown') as name
FROM users u
WHERE id = 10;
You could use:
SELECT COALESCE(
TRIM(CONCAT(COALESCE(u.user_fname,''), ' ', COALESCE(u.user_lname,''))),
'unknown') name
FROM users u
WHERE id = 10;
I always like the isnull function in sql server (or nvl in oracle):
SELECT isnull(u.user_fname + ' ','') + isnull(u.user_lname, '') name
FROM users u
WHERE id=10
but then to switch include the Nulls, I would use a case:
SELECT
CASE WHEN u.user_fname IS NULL AND u.user_lname is NULL THEN 'unknown'
ELSE isnull(u.user_fname + ' ','') + isnull(u.user_lname, '') END name
FROM users u
WHERE id=10
yes, it's a little longer than the other answers, but easier to read and perhaps more flexible in the future in case you have other conditions.
Not really a performance hit either way, so it's down to personal preference.
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;
I have a table with fullname column. I want to make a query for finding a person via his last name but his last name is in the full name column.
Would it matter if it accidentally returned someone whose first name matched your query?
A simple query would be:
SELECT *
FROM TABLE
WHERE fullname LIKE '%insertlastname%'
If you want to define the last name as the name after the last space:
SELECT substring_index(fullname, ' ', -1) as lastname
FROM TABLE
WHERE lastname='insertlastname'
Two suboptimal answers, but some answers at least.
enter code here You can use this if you want to fetch by query:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX( `fullname` , ' ', 2 ),' ',1) AS b,
SUBSTRING_INDEX(SUBSTRING_INDEX( `fullname` , ' ', -1 ),' ',2) AS c FROM `users` WHERE `userid`='1'
But you can also try by PHP to fetch last name. You just use explode function to fetch last name.
Exm:
$full_name = "row moin";
$pieces = explode(" ", $fullname);
echo $first_name = $pieces[0]; // row
echo $last_name = $pieces[1]; // moin
A simple answer for this is like this suppose we have a name
Charles Dickens
:
SELECT * FROM TABLE_NAME WHERE SUBSTRING_INDEX(FULLNAME,' ',-1) like '%Dickens';
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
There are two columns in a MySQL table: SUBJECT and YEAR.
I want to generate an alphanumeric unique number which holds the concatenated data from SUBJECT and YEAR.
How can I do this? Is it possible to use a simple operator like +?
You can use the CONCAT function like this:
SELECT CONCAT(`SUBJECT`, ' ', `YEAR`) FROM `table`
Update:
To get that result you can try this:
SET #rn := 0;
SELECT CONCAT(`SUBJECT`,'-',`YEAR`,'-',LPAD(#rn := #rn+1,3,'0'))
FROM `table`
You can use mysql built in CONCAT() for this.
SELECT CONCAT(`name`, ' ', `email`) as password_email FROM `table`;
change field name as your requirement
then the result is
and if you want to concat same field using other field which same then
SELECT filed1 as category,filed2 as item, GROUP_CONCAT(CAST(filed2 as CHAR)) as item_name FROM `table` group by filed1
then this is output
In php, we have two option to concatenate table columns.
First Option using Query
In query, CONCAT keyword used to concatenate two columns
SELECT CONCAT(`SUBJECT`,'_', `YEAR`) AS subject_year FROM `table_name`;
Second Option using symbol ( . )
After fetch the data from database table, assign the values to variable, then using ( . ) Symbol and concatenate the values
$subject = $row['SUBJECT'];
$year = $row['YEAR'];
$subject_year = $subject . "_" . $year;
Instead of underscore( _ ) , we will use the spaces, comma, letters,numbers..etc
In query, CONCAT_WS() function.
This function not only add multiple string values and makes them a single string value. It also let you define separator ( ” “, ” , “, ” – “,” _ “, etc.).
Syntax –
CONCAT_WS( SEPERATOR, column1, column2, ... )
Example
SELECT
topic,
CONCAT_WS( " ", subject, year ) AS subject_year
FROM table
I have two columns:
prenom and nom so to concatenate into a column with name chauffeur_sortant I used this script:
SELECT date as depart, retour, duree_mission, duree_utilisation, difference, observation, concat( tb_chaufeur_sortant.prenom, ' ', tb_chaufeur_sortant.nom) as chauffeur_sortant, concat(tb_chaufeur_entrant.prenom, ' ', tb_chaufeur_entrant.nom) as chauffeur_entrant
FROM tb_passation
INNER JOIN tb_vehicule
ON tb_vehicule.id = tb_passation.id_vehicule
INNER JOIN tb_chaufeur_sortant
ON tb_chaufeur_sortant.id = tb_passation.id_sortant
INNER JOIN tb_chaufeur_entrant
ON tb_chaufeur_entrant.id = tb_passation.id_entrant WHERE tb_vehicule.id = '';
$crud->set_relation('id','students','{first_name} {last_name}');
$crud->display_as('student_id','Students Name');