473,378 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

concat in select statement

Claus Mygind
571 512MB
Is it possible to conduct a test in the select statement to see if a value is not found in one table (t2) then get the value from another table (t3).

In this case you would have 3 tables.
if there is no related record in table2 t2, then get the value from table3 t3

If so what would the code look like?

Expand|Select|Wrap|Line Numbers
  1. select  t1.*, concat(   ) as aFoundValue
  2. from table1 t1
  3. left join table2 t2 on t1.key=t2.key
  4. left join table3 t3 on t1.key=t3.key
  5.  
Mar 27 '09 #1
2 2400
Atli
5,058 Expert 4TB
Hi.

You can use the IF and IFNULL functions to do that.

For example, if you have this data:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE t1(id Serial Primary Key, txtValue varchar(10));
  2. CREATE TABLE t2(id Serial Primary Key, txtValue varchar(10));
  3.  
  4. INSERT INTO t1(txtValue) VALUES('First'), (Null), ('First'), (NULL), (NULL), ('First');
  5. INSERT INTO t2(txtValue) VALUES(NULL), ('Second'), (Null), ('Second'), ('Second'), (Null);
Then you could do this:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   IF(IFNULL(t1.txtValue, true), 
  3.      t2.txtValue, 
  4.      t1.txtValue
  5.   ) AS 'Value'
  6. FROM t1 
  7. LEFT JOIN t2 
  8.   ON t1.id = t2.id;
Which would fill in the NULL fields from the first table with data from the second:
Expand|Select|Wrap|Line Numbers
  1. +--------+
  2. | Value  |
  3. +--------+
  4. | First  |
  5. | Second |
  6. | First  |
  7. | Second |
  8. | Second |
  9. | First  |
  10. +--------+
  11. 6 rows in set (0.00 sec)
Apr 8 '09 #2
Claus Mygind
571 512MB
Thank you very much for that very useful tip.
Apr 8 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Gerald Aichholzer | last post by:
Hello, I need to specify the following attribute in an xhtml-file containing TAL templates: <div tal:attributes="onMouseOver concat('func(',xyz,')')"> which results in <div...
2
by: ctb | last post by:
I am not sure how to, or even if you can do this. I have tried several things. I have a drop down list that is databound to a datareader. I wish to set the DataTextField to a concatentation of...
3
by: nickdevx | last post by:
Damn it this "illegal mix of collashit" messages are driving me nuts!! What's wrong with the following statement? SELECT userid, CONCAT(username,' / ',firstname,' ',lastname,' / ', email,...
4
by: Martin Evans | last post by:
Hi, I'm getting: DBD::DB2::db do failed: SQL0440N No authorized routine named "CONCAT" of type "FUNCTION" having compatible arguments was found. SQLSTATE=42884 for some SQL like this:
6
by: GarryJones | last post by:
I think the following statement .... $ml_collect='SELECT *, DATE(CONCAT(field1, field2)) AS thedate FROM ml_lopp LEFT JOIN scfmforening ON (scfmforening.scfmnum = ml_lopp.scfmnum) LEFT JOIN...
6
by: antmail | last post by:
Hi guys, I have spend several days now trying to solve this problem inside a stored procedure. I want to call a procedure providing 3 variables. The variables area used to select the appropriate...
2
by: cubix | last post by:
I'm trying to run a query in MySQL Query Browser that will concat 3 fields together to form a single date format. This part I don't have a problem with: SELECT CONCAT(`Field12`, '/', `Field13`,...
4
by: waqasahmed996 | last post by:
hi to all i have three fields of name in database named as fnam,mname,lname. fname and lname is mandatory field and mname is optional. i want to make a search query on name ...
5
by: tovachanah | last post by:
<xsl:param name="foostart" select=(xsl:value-of select="startdate")/> <xsl:param name="fooend" select =(xsl:value-of select= "enddate")/> <xs:param name="startend...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.