SELECT A.CUSTNAME|| WHO IS HOLDING ||A.ACCNAME|| ACCOUNT ||T.TRATYPE|| THE AMOUNT ||T.AMT||
ON || T.t_DATE
FROM ACCOUNTHOLDER A,TRANSACTION T
ON(T.ACCNO=A.ACCNO)
WHERE CUSTNAME='JAMES BOND'
/
I have two tables accountholder and transaction table where accno is common. accountholder accno is primary key and other is foreign key. I need the output in sentence form...
You are probably looking for CONCAT() function, and your custom parts of the sentence have to be enclosed in single quotes and have the desired spaces at the beginning and at the end:
SELECT CONCAT (
A.CUSTNAME,
' WHO IS HOLDING ',
A.ACCNAME,
' ACCOUNT ',
T.TRATYPE,
' THE AMOUNT ',
T.AMT,
' ON ',
T.t_DATE
)
FROM ACCOUNTHOLDER A
INNER JOIN TRANSACTION T ON T.ACCNO = A.ACCNO
WHERE CUSTNAME = 'JAMES BOND'
And your JOIN wasn't right either You either use the explicit way and add INNER JOIN and ON, or you separate your tables with commas and put the condition in the WHERE clause.
You need to use proper join syntax. In addition to the on clause, you need a join statement. In addition, what look like string constants should be in single quotes. And, the proper way to concatenate strings in MySQL is to use the concat() function, not vertical bars:
SELECT concat(A.CUSTNAME, ' WHO IS HOLDING ', A.ACCNAME, ' ACCOUNT ',
T.TRATYPE, ' THE AMOUNT ', T.AMT, ' ON ', T.t_DATE)
FROM ACCOUNTHOLDER A join
TRANSACTION T
ON T.ACCNO=A.ACCNO
WHERE CUSTNAME = 'JAMES BOND';
Related
How to remove all spaces between a column field?. The spaces occur in the middle of the text so trim won't work and also replace is not working.
my code is
UPDATE temp_emp t1, master_employee t2
SET t1.lm= t2.emp_id
where REPLACE(t1.lm, ' ', '') = REPLACE(CONCAT(t2.first_name,'',t2.last_name), ' ', '');
for example when i run the query ,
select REPLACE(lm, ' ', '') AS concat from temp_emp1
i get the output as follows
concat
----------------------------------------
rick joe
james cole
albert Th
i want the output to be ;like this
concat
----------------------------------------
rickjoe
jamescole
albertTh
Without knowing the table structures and data, it is difficult for me to follow what you are doing. However, to accomplish the ouput of two concatenated columns is very straightforward.
Assume you have a table master_employee with just two columns and you want to output the FIRST and LAST names concatenated with no spaces in between. You simply use the function concat()for MySQL:
SELECT CONCAT(first_name, last_name)
from master_employee;
In Oracle, the concatenation is two pipes (||):
SELECT first_name || last_name
from master_employee;
Hope this helps.
If you want to update the existing column which has multiple spaces into one then this update query will be helpful:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', ' ');
If you don't want any spaces then this should work:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', '');
Tried to group_concat a text field in mysql.But its taking only the first value.(if the values are '177,178') it taking only 177 because I am saving this as a text field.So how can I do group_concat with this fields?
My query looks as such:
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ' ') FROM abc WHERE abc.lookupId IN ('177,178')
Are you misplacing the quotes within your IN?
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ',') FROM abc WHERE abc.lookupId IN (177,178)
The problem isn't with GROUP_CONCAT, the problem is that your WHERE clause is only selecting one row. Since the lookupId field is an integer, it's converting the string '177,178' to an integer, which is just 177. You shouldn't have quotes around the values in the IN() list, that's making it just a single value to look for.
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ' ')
FROM abc
WHERE abc.lookupId IN (177, 178)
If the comma-separated string is actually coming from a column in a table you're joining with, see sql join tables where 1 column has comma.
SELECT GROUP_CONCAT(table1.displayValue SEPARATOR ' ')
FROM table1
JOIN table2 ON FIND_IN_SET(table1.lookupID, table2.lookupIDs)
WHERE ...
I'm trying to join two columns of the same table but, if there are null values (in my case they are in the second column), I want to take anyway the row that interests me and, rather than putting the null value, I would put ''. The columns that I want to join are Surname and Name. In other words, I tried to use :
SELECT CONCAT(CSurname, ' ', CName)
FROM Client;
In this way if I have a valid value for surname and a null value for name I obtain null. I use MySql, thanks.
If you want to avoid the problem with a leading space, then the easiest way is probably CONCAT_WS():
SELECT CONCAT_WS(' ', CSurname, CName)
FROM Client;
Unlike most other functions, CONCAT_WS() ignores NULL values (except for the separator), greatly simplifying this logic -- particularly when you are working with more than two columns.
Without it, the equivalent logic could be expressed as:
SELECT CONCAT(COALESCE(CONCAT(CSurname, ' '), ''), COALESCE(CName, ''))
Try the ifnull function
SELECT CONCAT(CSurname, ' ', IFNULL(CName,'')) FROM Client;
I don't have a local mysql installation to try it out but the IFNULL function should achieve what you need.
I have the query to select some columns from two tables, which must contain some cells which are actually empty but have to name it in query such as
select name, REPLACE(format, ' – ', '/') as format, CAST(price AS UNSIGNED INTEGER) as price, CONCAT('http://example.com/',url_key) as url, level_id_value, delivery, sku from t1 where delivery <> 'slut' INTO OUTFILE 'test.txt' FIELDS TERMINATED BY '\t';
which perfectly works producing 2.5 MB size text file in lib folder but the other code as follows for another query where i have to add one column from another table along with some empty columns with some title, seems to work but produces a file size of around 11GB,
sELECT t1.sku, t.name, ' ' as Size, format, ' ' as subcategory, CAST(t1.price AS UNSIGNED INTEGER) as price, ' ' as dummy, CONCAT('http://example.com/',t1.url_key) as url, CONCAT('Från ',t1.level_id_value) as shipping_fee, t2.is_in_stock as stock, ' ' as text1, ' ' as Description from t2,t1 where t1.delivery <> 'slut' INTO OUTFILE 'test.txt' FIELDS TERMINATED BY '\t';
any help to debug this issue would be appreciable, don know much about mysql, so detailed explanation are highly welcome. Is there a way where i can make the code analogous to the first working code from two tables? I just need one column from t2 table, that is is_in_stock.
So the first one is a simple query SELECT INTO with a WHERE clause, this is going to return a small number of results.
The second one is showing an implicit inner join, but you arent returning the values based on a unique row, you just pull in everything from both tables, regardless of whether they are matching in both tables
SELECT t1.sku,
t.name,
' ' as Size,
format,
' ' as subcategory, CAST(t1.price AS UNSIGNED INTEGER) as price,
' ' as dummy, CONCAT('http://example.com/',t1.url_key) as url,
CONCAT('Från ',t1.level_id_value) as shipping_fee,
t2.is_in_stock as stock,
' ' as text1,
' ' as Description **`from t2,t1`**
where t1.delivery <> 'slut' INTO OUTFILE 'test.txt' FIELDS TERMINATED BY '\t';
This one is how I would do it, but you still need to isolate each record, so what makes each row unique, and do the inner join ON those fields between t1 and t2 like inner join t2 ON t1.name = t2.name and t1.id = t2.id but it should look something like this
SELECT t1.sku,
t1.name,
t1.format,
CAST(t1.price AS UNSIGNED INTEGER) as price,
CONCAT('http://example.com/',t1.url_key) as url,
CONCAT('Från ',t1.level_id_value) as shipping_fee,
'' as dummy,
'' as size,
'' as subcat,
'' as text1,
'' as Description,
t2.is_in_stock as stock,
from t1
INNER JOIN t2 WHERE t1.delivery <> 'slut'
INTO OUTFILE 'test.txt' FIELDS TERMINATED BY 't\';
Hope it helps!
I have PHP 5 code accessing a MyISAM table on MySQL 5 server. The query looks like this:
SELECT CONCAT(fName1,' ',mName2,' ',lName3) AS userName
FROM users
WHERE level > 10
When there's no mName filled in, I am expecting output like "fname lname" , but I'm getting "" (empty string) instead (the number of rows returned is correct). Where am I making a mistake?
PHP code:
<?php
$result = mysql_query($the_above_query);
while ($result_row = mysql_fetch_assoc($result)) {
// do stuff with the name
// except I'm getting empty strings in $result_row['userName']
}
Relevant part of table structure:
CREATE TABLE users {
/* -snip- */
`fName1` varchar(50) default NULL,
`mName2` varchar(50) default NULL,
`lName3` varchar(50) default NULL,
`level` int(11) default 0,
/* -snip- */
} ENGINE=MyISAM DEFAULT CHARSET=utf8;
(also, is this way (column concatenation in MySQL) a good idea, or should I fetch the columns to PHP and join them there?)
Turns out that I was getting back a NULL; PHP treats a returned NULL and empty string("") similarly, you'd have to compare with === to see the difference.
From google: http://bugs.mysql.com/bug.php?id=480
[23 May 2003 4:32] Alexander Keremidarski
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.mysql.com/documentation/ and the instructions on
how to report a bug at http://bugs.mysql.com/how-to-report.php
This is doccumented behaviour of CONCAT() function.
From Manual chapter 6.3.2 String Functions
CONCAT(str1,str2,...)
Returns the string that results from concatenating the arguments. Returns NULL if any
argument is NULL
Use CONCAT_WS() instead or wrap NULLable paremeters with IFNULL() function.
Documentation and usage for CONCAT_WS: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat-ws
From MYSQL docs
CONCAT() returns NULL if any argument
is NULL.
you want to use CONCAT_WS()
CONCAT_WS(separator,str1,str2,...)
But best bet is to just pull it back and use php cause if you need a different format or just one of those fields later you'll have to make another db call
In MySQL concatenating any string to a NULL value results in NULL.
You have to check for NULL before concatenate using IFNULL:
SELECT CONCAT(IFNULL(fName1,''),' ',IFNULL(mName2,''),' ',IFNULL(lName3,'')) AS userName
FROM users
WHERE level > 10
This was the solution I came up with which included Keeper and Ersatz answer. System would not allow me to vote you guys up though :(
CONCAT_WS(IFNULL(ts_usr_nameDetails.first_name,''),' ',IFNULL(ts_usr_lib_connectionNameDetails.first_name,'')) AS composerName
This allowed for some amazing results
This is an answer based on the solution above by #chocojosh and another question here: MySQL/SQL: Update with correlated subquery from the updated table itself
I had a similar problem, but I was trying to concat a bunch of group_concats and some were NULL which caused the whole row to be NULL. The goal was to place data from other tables into a single field in the main table to allow fulltext searches.
Here is the SQL that worked. Note the first parameter for concat_ws I made ' ', this allowed for spaces between the values for the fulltext field.
Hope this helps someone.
update
products target
INNER JOIN
(
select p.id,
CONCAT_WS(
' ',
(select GROUP_CONCAT(field SEPARATOR ' ') from table1 where productId = p.id),
p.title,' ',
(select GROUP_CONCAT(field, ' ', descriptions SEPARATOR ' ') from table2 where productId = p.id),
(select GROUP_CONCAT(field SEPARATOR ' ') from table3 where productId = p.id),
(select GROUP_CONCAT(field, ' ', catno SEPARATOR ' ') from table4 where productId = p.id),
(select GROUP_CONCAT(field SEPARATOR ' ') from table5 where productId = p.id),
(select GROUP_CONCAT(field SEPARATOR ' ') from table6 where productId = p.id),
(select GROUP_CONCAT(field SEPARATOR ' ') from table7 where productId = p.id)
) as ft
from products p
) as source
on target.id = source.id
set target.fulltextsearch = source.ft
you could also the COALESCE() function to return the first non-null value. Like so:
SELECT CONCAT(fName1,COALESCE(CONCAT(' ',mName2,' '),' '),lName3) AS userName
FROM users
WHERE level > 10
This would also only put one space if there was no middle name and a space before and after if there was a middle name.
Reference for this function can be found at: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce