I've two tables say ABC and XYZ,
Table ABC:
B C ID ABC_ID
====================
50 30 10 2
60 31 11 3
Note: All the value fields are integers.
Table XYZ:
group condition value ID
================================
B = 50 10
C > 30 11
Note:
Column B and condition are varchar strings and value, ID columns are integers.
Using MySQL 5.5
I'm joining both the tables on the basis of column, ID.
There are many values for group in table XYZ and those values are columns in table ABC.
So, my questions are:
Once the ID matches, I will get the value of group as B in the above case from table XYZ. Then using the value, how should I get the value of B from table ABC?
I want to do something like :
select A.(X.group) from ABC A join XYZ X on A.ID = X.ID;
Once I'm able to get the value of column B using step 1 (as above), I need to form an expression like (group condition value) e.g. in able case B = 50 and replace the value of B from table ABC in the expression. The formed expression needs to be evaluated and if true, need to return the ABC_id from table ABC. How can I evaluate the expression?
I'm doing something like this:
select A.ABC_ID from XYZ X join ABC A on A.ID = X.ID where X.(A.group) A.condition A.value;
I've tried using PREPARE'ing and EXECUTE'ing but the join of ABC and XYZ would give multiple rows as a set. And it does not work.
SELECT X.group, X.condition, X.value
from XYZ X join ABC A
on X.ID = A.ID
into #colname, #condition, #value;
SET #qry = CONCAT('SELECT * from ABC WHERE ',#colname,#condition,#value);
PREPARE stmt FROM #qry;
EXECUTE stmt;
Thanks.
I created a sqlfiddle of this here: http://sqlfiddle.com/#!2/15e205/5
Note that I changed the names of group and condition because those are reserved words, and really aren't good choices for column names.
I'm using CONCAT() to build the query based on the values retrieved in XYZ.
SELECT grp, cndition, value
from XYZ
where id = 10
into #colname, #condition, #value;
SET #qry = CONCAT('SELECT * from abc WHERE ',#colname,#condition,#value);
PREPARE stmt FROM #qry;
EXECUTE stmt;
Related
Assuming I have the following two tables:
**Table A**
ID Day Month Year
------------------------
1 1 1 1900
3 13 3 2009
49 28 2 1984
**Table B**
ID ABC_1_1_1900 ABC_2_1_1900 ... ABC_31_12_2100
-------------------------------- ... ---------------
1 431 15449 98565
2
3 ....
.
.
n ....
and would like to get the following table:
**Table C**
ID ABC
------------------------
1 431
3 (value B.ABC_13_3_2009 for ID=3)
49 (value B.ABC_28_2_1984 for ID=49)
What essentially I'm trying to achieve is, get a subset of Table B by matching the row contents of A to the column names of B and inner joining on the IDs.
The equivalent would be
SELECT A.ID, B.CONCAT('ABC_', A.Day, '_', A.Month, '_', A.Year) AS ABC
FROM A
INNER JOIN B ON A.ID=B.ID
which unfortunately doesn't work. Any ideas greatly appreciated!
What you are trying to do is to create an SQL statement from a variable. How this is done depends on the SQL technology you are using. From the tag, I assume that you use MySQL. With MySQL you need to make a so called Prepared Statement (Attention: They say it's quite hacky, see: mysql field name from variable). With the prepared statement, you will be able to pass the column contents to your SQL statement. All this would look like the following:
Create SQL String:
SET #s = CONCAT('SELECT A.ID, B.ABC_', A.Day, '_', A.Month, '_', A.Year, ' AS ABC FROM A INNER JOIN B ON A.ID=B.ID');
Prepare Statement and execute:
PREPARE stmt FROM #s;
EXECUTE stmt;
Note: Be aware that this can be prone to errors, since the number in the column (for example day) must always be formatted the same as the number in the column name (ABC_X_Y_Z).
I want count the length of a comma separated column
I have use these
(LENGTH(Col2) - LENGTH(REPLACE(Col2,",","")) + 1)
in my select query.
Demo:
id | mycolumn
1 2,5,8,60
2 4,5,1
3 5,Null,Null
query result for first two row is coming correctly.for 1 = 4 ,2 = 3 but for 3rd row it is calculating null value also.
Here is what I believe the actual state of your data is:
id | mycolumn
1 2,5,8,60
2 4,5,1
3 NULL
In other words, the entire value for mycolumn in your third record is NULL, likely from doing an operation involving a NULL value. If you actually had the text NULL your current query should still work.
The way to get around this would be to use COALESCE(val, "") when handling the NULL values in your strings.
Crude way of doing it is to replace the occurances of ',Null' with nothing first:-
SELECT a.id, (LENGTH(REPLACE(mycolumn, ',Null', '')) - LENGTH(REPLACE(REPLACE(mycolumn, ',Null', ''),",","")) + 1)
FROM some_table a
If the values refer to the id of rows in another table then you can join against that table using FIND_IN_SET and then count the matches (assuming that the string 'Null' is not an id on that other table)
SELECT a.id, COUNT(b.id)
FROM some_table a
INNER JOIN id_list_table b
ON FIND_IN_SET(b.id, a.mycolumn)
GROUP BY a.id
I want to get the data from 1 SQL query of a table with 2 values
select c from tmp;
c
foo
bar
2 rows in set (0.00 sec)
The returned data I need is <foo,bar>
Concat does not do this nor does any string function I can find. I can SUM integers from 2 lines. Why can't I retrieve the string values likewise?
you can use GROUP_CONCAT() function to get values together.
This will combine all the string values separated by comma
Perhaps you need a group_concat() in MYSQL.
Noticing that you need in MYSQL here a sample:
* SQLFIDDLE demonstration
Select department, group_concat(name,',') as nameList
from foo
group by department
;
Results:
Department NameList
D1 John, Mary
D2 Tim, Dan, Jack
D3 Kate, Felix
Following is a method to use in TSQL:
You can try the following sample code and adjust it for your table/columns:
SELECT department, namelist = STUFF(
(SELECT ','+ Name FROM foo B
WHERE b.department = a.department FOR XML PATH('')) , 1 , 1 , '' )
FROM foo A
Or else you may do a CTE.
Hi I have a table with name test. it got 7 columns id , a , b , c , d , e , f. All this columns contains either 1 or 0. Now i want make a query where i can choose only those columns whose value is 1.
Something like this:
select (condition) from test where id = 5;
because i have a hotel table with 50 columns out of which 11 columns contains either 1 or 0 representing the facilities of the hotel. I want to make a query which just tells what are the facilities of the hotel.
Any help would be great.
select id, (a*64)+(b*32)+(c*16)+(d*8)+(e*4)+(f*2)+(g*1)
from test
this number you can reverse it to convert to a 7 digit binary code.
examples:
18 = 0010010 , 1000000 = 64
using sql you can select rows, NOT columns
If it is that what you want you can bulid your query like this
select id, a, b, c, d -- columns to select
from test -- table
where (a = 1 or b=1 or c = 1 or d = 1) -- these are the conditions
Got this:
Table a
ID RelatedBs
1 NULL
2 NULL
Table b
AID ID
1 1
1 2
1 3
2 4
2 5
2 6
Need Table a to have a comma separated list as given in table b. And then table b will become obsolete:
Table a
ID RelatedBs
1 1,2,3
2 4,5,6
This does not rund through all records, but just ad one 'b' to 'table a'
UPDATE a, b
SET relatedbs = CONCAT(relatedbs,',',b.id)
WHERE a.id = b.aid
UPDATE: Thanks, 3 correct answers (marked oldest as answer)! GROUP_CONCAT is the one to use. No need to insert commas between the ids using relatedids = CONCAT(relatedids,',',next_id) that is done automatic by GROUP_CONCAT.
You'll have to use the mysql group_concat function in order to achieve this: http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_group-concat
Look into GROUP_CONCAT(expr)
mysql> SELECT student_name,
-> GROUP_CONCAT(DISTINCT test_score
-> ORDER BY test_score DESC SEPARATOR " ")
-> FROM student
-> GROUP BY student_name;
You can't do that in standard SQL. You could write a stored procedure to do that. I had a similar problem, but I was using PostgreSQL so I was able to resolve it by writing a custom aggregate function so that you can do queries like
select aid, concat(id)
from b group by
aid
Update: MySQL has a group_concat aggregate function so you can do something like
SELECT id,GROUP_CONCAT(client_id) FROM services WHERE id = 3 GROUP BY id
as outlined here.