Append text to each row of the sql select query - sql-server-2008

I have a query like this
SELECT COUNT(ID) 'Records Affected', TYPE FROM MASTER
GROUP BY TYPE
The output for this is
Records Affected TYPE
---------------- ----
4 F1
3 F2
5 F3
Now I would like to change the query so that the output will be as follows
Records Affected
----------------
The number of records affected for F1 is : 4
The number of records affected for F2 is : 3
The number of records affected for F3 is : 5
"The number of records affected for " + TYPE + " is : " + COUNT.
How can I add the default text to each row of the result set instead of appending in the front end. I would like to simplify my task of just showing the records in the DataGrid as Summary.

You can easily concatenate the string using the following. You will use the + to concatenate the string to the type column and the count. Note, the count needs to be converted to a varchar for this to work:
SELECT
'The number of records affected for '+ type +
' is : '+ cast(COUNT(ID) as varchar(50)) as'Records Affected'
FROM yt
GROUP BY TYPE;
See SQL Fiddle with Demo

Just put the text in your query:
SELECT 'The number of records affected for ' + TYPE + ' is : ' + CAST(COUNT(ID) as VARCHAR(20)) AS 'Records Affected' FROM MASTER
GROUP BY TYPE

SELECT "The number of records affected for " + TYPE + " is : " + COUNT(ID) AS [Records Affected]
FROM Master
GROUP BY TYPE

Use this query:
UPDATE bookmark_linx SET link_url=(SELECT CONCAT(link_url, '?raw=true')) WHERE link_url LIKE '%dropbox%'

Try this:
SELECT 'The number of records affected for ' + TYPE + ' is : ' +
STR(X.[Records Affected]) AS [Records Affected]
FROM (SELECT COUNT(ID) 'Records Affected', TYPE FROM MASTER GROUP BY TYPE) X

Related

masking the id of an employee in a select query

I have an ID which I want to mask the last 4 digits of that id.
example: my_id = 123456789
SELECT
concat(left(my_id,length(my_id) -4)) + ' ' + 'xxxx' AS masked_data
FROM
dual ;
Expected output : 12345XXXX
But I am getting as 12345
The strings that you're concatenating should all be arguments to the CONCAT() function. + is for addition, not concatenation.
SELECT
concat(left(my_id, length(my_id) - 4), 'xxxx') AS masked_data
You also don't want a space before xxxx.

MySQL, Finding every string/words frequency in a column

I want to find every word frequency in a column by using MySQL only (if possible). For example:
Table:
id message
1 I want to eat pizza
2 I wanted chocolates
3 He doesn't like me
Query: ???
Result:
Word Frequency
I 2
want 1
to 1
eat 1
pizza 1
wanted 1
etc..
Is it possible? If so please help, thank you
You need to split the data. This is a pain:
select substring_index(substring_index(message, ' ', n.n), ' ', -1) as word,
count(*)
from (select 1 as n union all select 2 union all select 3 union all
select 4 union all select 5
) n join
t
on n.n <= 1 + length(message) - length(replace(message, ' ', ''))
group by word;
The above assumes that all messages are five words or less. You can extend the number in the first subquery for longer messages.
Here is a php example. You will probably have to tweak it a bit.
lets assume you have a word_frequency table with a unique column word and an integer for count. Also, this is susceptible to SQL injection, so you should be careful. But this should get you started.
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = mysqli_query($con,"SELECT message FROM table1");
while($row = $results->fetch_assoc()) {
$words = explode(" ", $row['message']);
foreach ($words as $word) {
mysqli_query($con,"INSERT INTO word_frequency (`word`,`count`) VALUES ('$word',1) ON DUPLICATE KEY UPDATE `count`=`count`+1;");
}
}
mysqli_close($con);

UPDATE multiple table with same column name with condition for each value on MYSQL

I've a column "pid" and a lot of table on each database , then I want to update all table from every database with column "pid" and set 'pid' = 5 where 'pid' = 3 and set 'pid' = 6 where 'pid = '7' on 1 query .
I've find post like this and try to apply it:
Select 'UPDATE ' + TABLE_NAME + ' SET pid = ''5'' '
From INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'pid'
Without condition for try it , then MYSQL just return me a select with value 0 .
'UPDATE ' + TABLE_NAME + ' SET pid = ''5'' '
0
Need a little help and explain for make this query work and understand how its work .
Really thanks everyone :)
UPDATE table_name
SET pid = 5
WHERE pid = 3;
It should be implemented like this. You simply tell to update the table named table_name to set pid as '5' wherever it finds it as '3'.
Read also here. ;)

How can I extract a last name from full name in mysql?

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';

different results for two queries

Query 1 :
SELECT
SUM(aol_int) AS AOL,
SUM(android_phone_int) AS Android_Phone,
SUM(androidTablet_int) AS Android_Tablet,
SUM(apple_mail_int) AS Apple_Mail,
SUM(blackberry_int) AS Blackberry,
SUM(Eudora_int) AS Eudora,
SUM(gmail_int) AS Gmail,
SUM(hotmail_int) AS Hotmail,
SUM(lotus_notes_int) AS Lotus_Notes,
SUM(other_int) AS Other,
SUM(other_webmail_int) as Other_Web_Mail,
SUM(Outlook_int) AS Outlook,
SUM(Postbox_int) AS Postbox,
SUM(sparrow_int) AS Sparrow,
SUM(thunderbird_int) AS Thunderbird,
SUM(windowsLiveMail_int) AS Windows_Live_Mail,
SUM(yahoo_int) AS Yahoo,
SUM(iPad_int) AS iPad,
SUM(iPhone_int) AS iPhone,
SUM(iPod_int) AS iPod
FROM mytable;
Query 2:
select sum(aol_int + android_phone_int + androidtablet_int+apple_mail_int+blackberry_int+Eudora_int+gmail_int+hotmail_int+lotus_notes_int+other_int+other_webmail_int+Outlook_int+Postbox_int+sparrow_int+thunderbird_int+windowsLiveMail_int+yahoo_int+iPad_int+iPhone_int+iPod_int)
as total_percentage
FROM mytable;
When I am summing up the results of Query 1 I am getting different sum as compared to what I am getting via Query2. The value in Query2 is less than Query 1. Why is it like that?
TROUBLESHOOTING:
I tried to write my query like this:
SELECT SUM( ISNULL(aol_int,0) +
ISNULL(android_phone_int,0) +
ISNULL(androidtablet_int,0) +
ISNULL(apple_mail_int,0) +
ISNULL(blackberry_int,0) +
ISNULL(Eudora_int,0) +
ISNULL(gmail_int,0) +
ISNULL(hotmail_int,0) +
ISNULL(lotus_notes_int,0) +
ISNULL(other_int,0) +
ISNULL(other_webmail_int,0) +
ISNULL(Outlook_int,0) +
ISNULL(Postbox_int,0) +
ISNULL(sparrow_int,0) +
ISNULL(thunderbird_int,0) +
ISNULL(windowsLiveMail_int,0)+
ISNULL(yahoo_int,0) +
ISNULL(iPad_int,0) +
ISNULL(iPhone_int,0) +
ISNULL(iPod_int,0) )AS total_percentage
FROM mytable;
However, I am getting an error after running above query in the MySQL workbench:
Error Code: 1582. Incorrect parameter count in the call to native function 'ISNULL'. What is wrong here?
This could happen if some columns in some of the rows contain nulls. When this happens, all columns in a row with even a single null column would produce null in the chain of additions, so the row will add nothing to the total.
Here is a short demo of this effect. Setup:
create table test(x int null, y int null);
insert into test(x,y) values (1,null);
insert into test(x,y) values (null,2);
insert into test(x,y) values (3,3);
Queries:
select sum(x+y) from test; -- Shows 6
select sum(x)+sum(y) from test -- Shows 9
See this demo on sqlfiddle: link.