Use value of column as table name in subquery mysql - mysql

Hi I am new to mysql I am trying to to create a query where the subquery table name is based in value of column name
I got three table as sample only can be more
first table named main
Main
id tbl val
1 t 2
2 r 1
Next table named t
name
ana
jjj
Next table named r
name
ana
hyu
Desired out put
I tried this query but not working sql syntax erro
id tbl val test
1 t 2 exist
2 r 1 not
Select id, tbl, val, #tblname := tbl,
isnull((select 'exist' from #tblname where name="jjj" limit 1), "not") as test
from main
will really appreciate any advise or help thank you

Related

What will be Mysql query for group by and order by dictionary style?

Trying to fetch data and print as like dictionary.
Table:
blog_tags
id name
1 atag1
2 atag2
3 dtag1
4 etag1
5 etag2
6 ctag1
7 ctag2
8 ctag3
9 ztag1
I want the data output as:
A
atag1
atag2
C
ctag1
ctag2
D
dtag1
E
etag1
etag2
Z
ztag1
Started with this:
select name from blog_tags order by name;
what will be mysql query for this?
Try something like this
select name
from (
select distinct upper(substring(name, 1, 1)) as name
from blog_tags
union all
select name
from blog_tags
)
order by name
Edit
If you want to get raw data for application level manipulation, I would suggest querying the db this way
select upper(substring(name, 1, 1)) as key,
name
from blog_tags
order by 1, 2
You can use the bellow query to achieve this as given below
select substr(name,1,1),group_conact(name) from blog_tags group by substr(name,1,1);
This query will group the name's by first character and will group concat the name's as comma separated. You can convert the result from your programming language to array
The output will be like given below
substr(name,1,1) group_conact(name)
A atag1,atag2
C ctag1,ctag2

whether to use pivot or non-pivot without any aggregate function

I got two tables table 1 and 2 as shown below. I need to insert the Std_rid for the row which has A and its corresponding Act_rid present in table 2 in resultant table 3. I'm only using the sample here. I'm very confused whether to use pivot or unpivot here. Any help appreciated. Thanks
Table 1
----------
G_Standard N_Standard Skill One_for_all Am_Water B2Future Std_rid
ELW.6 Lit.W.K.6 Writing A 1
ELW.8 Lit.W.K.8 Writing A 2
ELW.7 Lit.W.K.7 Writing A 3
Table 2
----------
Act_rid Act_Desc date createdby
3 Am_Water 2/4/15 sys
6 B2Future 2/4/15 sys
1 One_for_all 2/14/15 sys
Output Result Table 3
----------
ID Std_rid Act_rid
1 1 3
2 2 6
3 3 1
This is an UNPIVOT. UNPIVOT turns columns into rows.
;WITH Unpivot_Table_1 AS (
SELECT Std_rid,
Act_Desc
FROM [Table 1]
UNPIVOT (Value FOR Act_Desc IN ([One_for_all],[Am_Water],[B2Future])) u
WHERE u.Value = 'X'
)
SELECT ROW_NUMBER() OVER(ORDER BY t1.Std_rid) AS ID,
t1.Std_rid,
t2.Act_rid
FROM Unpivot_Table_1 t1
JOIN [Table 2] t2
ON t2.Act_Desc = t1.Act_Desc;
I hope this is something for data integration. Otherwise, y'all need to shoot whomever designed this.
Test data:
CREATE TABLE dbo.[Table 1]
(
Std_rid INT NOT NULL PRIMARY KEY,
One_for_all VARCHAR(1),
Am_Water VARCHAR(1),
B2Future VARCHAR(1)
);
INSERT INTO dbo.[Table 1] (Std_rid,One_for_all,Am_Water,B2Future)
VALUES
(1,NULL,'X',NULL),
(2,NULL,NULL,'X'),
(3,'X',NULL,NULL);
CREATE TABLE dbo.[Table 2]
(
Act_rid INT NOT NULL PRIMARY KEY,
Act_Desc VARCHAR(30)
);
INSERT INTO dbo.[Table 2] (Act_rid,Act_Desc)
VALUES
(3,'Am_Water'),
(6,'B2Future'),
(1,'One_for_all');

MYSQL query to Update the field if found duplicates?

Here is my problem. I got a table Meaning
ID - Meaning
1 - red car
2 - cat man
3 - red car
4 - ontime
5 - red car
....
I want to make the colum Meaning become Unique. So i want to build a query to found all the duplicates & for each of duplicate, the system should append [number] to make the cell become unique.
So after running that query, the result should be:
ID - Meaning
1 - red car
2 - cat man
3 - red car [2]
4 - ontime
5 - red car [3]
....
The table is pretty long about 100K rows. The query could be similar to this query
Update Table Meaning set meaning=concat(meaning,"1")
where meaning in (select meaning from Meaning group by meaning having count(meaning>1)
So what is the query for solving the problem?
Seem we have to use set variable to check each row?
step 1: create temporary table
CREATE TABLE TMP (id int, meaning varchar (2));
step 2: prepare query and insert into temporary table
insert into tmp
SELECT id,
CASE WHEN cnt =0 theN meaning ELSE concat(meaning,'[',cnt+1,']') END AS meaning
FROM
(
SELECT t1.id, t1.meaning, (
SELECT COUNT( t.id )
FROM test t
where t.meaning=t1.meaning
and t.id<t1.id
) as cnt
FROM test t1
)TMP
step 3
truncate table test
step 4: migrate to original
insert into test select * from tmp
SELECT x.*
, CONCAT(x.meaning,CASE WHEN COUNT(*) = 1 THEN '' ELSE COUNT(*) END) meaning
FROM meanings
x JOIN meanings
y ON y.meaning = x.meaning
AND y.id <= x.id
GROUP
BY id;

How to select a column names of a table in mysql based on the value it contains

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

mysql select update

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.