I would like to convert my query into SQLalchemy:
SELECT sel.ID,sel.URL,GROUP_CONCAT(sel.label_values)
FROM
(
SELECT table1.id as ID, table1.url as URL,CONCAT_WS(':',label
,value) as label_values
FROM table1 LEFT OUTER JOIN table2 ON table1.id =
table2.media_id
WHERE table1.type = "image" AND table1.id >= 0
ORDER BY table1.id
) AS sel
GROUP BY sel.ID
I tried something like this:
label_and_values_column = func.CONCAT_WS(' : ',table2.c.label,
table2.c.value).label(name='label_and_values_column')
outer_join_select = select([table1, label_and_values_column])\
.order_by(table1.c.id)\
.select_from(table1.outerjoin(table2))\
.label(name='outer_join_select')
all_ = func.group_concat(' : ',outer_join_select.id)\
.label(name='all_label_and_values_per_media_id')
The last line gives me
Caught exception: Neither 'Label' object nor 'Comparator' object has an attribute 'id'.
I am using db connection with transaction with engine.begin() as db_conn_transaction:
meta = sqlalchemy.MetaData(db_conn_transaction)
I would like to construct select_statment and then use select_statment.execute().fetchall()
Can't use .subquery() since I don't have session.query()...
Any help for converting my query in SQLalchemy is welcomed!
Related
I am getting incomplete string value from mysql query result. It appears that when the string is too long, the value is sliced. I am fetching the value as a JSON string. There is no issue with my local database, no matter how long the field value is.
Here's my query:
CREATE DEFINER=`devdbuser`#`localhost` PROCEDURE `GetTrainingFilesByID`(IN trainingID int)
BEGIN
SELECT t1.*, concat(t2.firstname,' ',t2.lastname) as username
FROM users t2
INNER JOIN
(
SELECT t.*,
CONCAT('[',
GROUP_CONCAT(CONCAT('{"id":"',d.id,
'","name":"', d.title,
'","file_path":"',d.doc_path,
'","is_video":"',d.is_video,'"}'
)), ']'
) files
FROM training t
LEFT JOIN training_documents d
ON t.id = d.training_id
GROUP BY t.id
) t1
ON t1.updated_by = t2.id
WHERE t1.id = trainingID
LIMIT 1;
END
The issue is with files field value.
Here's the incomplete output:
"[{"id":"1", "name":"Branch Workflow Model for GBD_1.pdf", "file_path":"1455443689.pdf", "is_video":"0"},
{"id":"2", "name":"http://192.168.11.32/GBD-Videos/testvideo.mp4", "file_path":"http://192.168.11.32/GBD-Videos/testvideo.mp4", "is_video":"1"},
{"id":"6", "name":"COD-CC-CrowdWisdom-Report-exclusive_mybigcommerce_com-2018_09_26-05_28_58.pdf", "file_path":"1862665875.pdf", "is_video":"0"},
{"id":"7", "name":"https://www.youtube.com/watch?v=rCAIY5n1hPA.mp4", "file_path":"https://www.youtube.com/watch?v=rCAIY5n1hPA.mp4", "is_video":"1"},
{"id":"8", "name":"https://www.youtube.com/watch?v=rCAIY5n1hP1.mp4", "file_path":"https://www.youtube.com/watch?v=rCAIY5n1hP1.mp4", "is_video":"1"},
{"id":"19", "name":"Branch Workflow Model for GBD.docx", "file_path":"1250685453.docx", "is_video":"0"},
{"id":"20", "name":"COK_CorporateMedicineDelivery_Growbydata.pdf", "file_path":"1245383653.pdf", "is_video":"0"},
{"id":"21", "name":"COK_Intro_Growbydata.pdf", "file_path":"1918218679.pdf", "is_video":"0"},
{"id":"22", "name":"http:/]"
Does mysql have some feature that prevents long field result values? How do I fix this?
Its main because you are using group concat. i recommend you to run this query so that it will increase the limit of group concat
SET GLOBAL group_concat_max_len=15000
hope this helps. :)
there is a json string in the field kv of one spark sql table;
such as
{"cpu_max_freq":"1401000","net_ok":"2","ad_report_status":"1\"}"}
how to select this line by using sql?
I have tried :
select * from table_a where get_json_object(kv, "$.ad_report_status") = '1\\"}'
select * from table_a where get_json_object(kv, "$.ad_report_status") = '1\"}'
select * from table_a where get_json_object(kv, "$.ad_report_status") = '1\\\"}'
select * from table_a where get_json_object(kv, "$.ad_report_status") = '1\\\"}'
none of above sql works !!
so how to match the value of "ad_report_status" field ?
select m1.id, m1.status, at.view_data, at.view_graph, ta.tag_string
from
access_tbl at, image_campaign_tbl m1
RIGHT JOIN
(select
GROUP_CONCAT(t.name) as tag_string , c.image_campaign_id
from campaign_tags_tbl c,tag_tbl t
where c.tag_id=t.id
$tag_q
group by c.image_campaign_id
) as ta
ON ta.image_campaign_id=m1.id
where
m1.client_id =$client_id
and m1.client_id = at.client_id
$prev_filter
limit $start,$end;
Error message:
in LOGS: DBD::mysql::db selectall_arrayref failed: Unknown column 't.name' in 'where clause' at /home/sakthi/rtads/Project/pm/Image/UI.pm line 2536.**
In Perl Module, I'm passing the same value of $tag_q to the $prev_filter to get the Pagination of filter based on TAGS values in the next page
if ( $prev_filter eq '' ) {
$prev_filter =
$search_clist_q . ' '
. $tag_q . ' '
}
From the error msg, I got the error which I'm doing. Since I'm trying to access the table of subquery in the main query, this error is happening.
So I want to know how to access the tag_string(or)t.name outside the subquery.
First of all, i suggest you to avoid use of old school syntax for jointures (FROM table1, table2,... WHERE table1.column1 = table2.column2 AND ...).
Here is the query that seems to return what you're looking for:
SELECT IC.id
,IC.status
,A.view_data
,A.view_graph
,TA.tag_string
FROM access_tbl A
INNER JOIN image_campaign_tbl IC ON IC.client_id = A.client_id
AND IC.client_id = $client_id
RIGHT JOIN (SELECT CT.image_campaign_id
,GROUP_CONCAT(T.name) AS [tag_string]
FROM campaign_tags_tbl CT
INNER JOIN tag_tbl T ON T.id = CT.tag_id
GROUP BY CT.image_campaign_id) TA ON TA.image_campaign_id = IC.id
WHERE <Your filters here>
LIMIT $start, $end
Hope this will help you.
Can someone tell me why this insert is failing but not giving me an error either? How do I fix this?
merge table1 as T1
using(select p.1,p.2,p.3,p.4,p.5 from #parameters p
inner join table1 t2
on p.1 = t2.1
and p.2 = t2.2
and p.3 = t2.3
and p.4 = t2.4) as SRC on SRC.2 = T1.2
when not matched then insert (p.1,p.2,p.3,p.4,p.5)
values (SRC.1,SRC.2,SRC.3,SRC.4,SRC.5)
when matched then update set t1.5 = SRC.5;
The T1 table is currently empty so nothing can match. The parameters table does have data in it. I simply need to modify this merge so that it checks all 4 fields before deciding what to do.
You can't select from a variable: from #parameters
See the following post: Using a variable for table name in 'From' clause in SQL Server 2008
Actually, you can use a variable table. Check it out:
MERGE Target_table AS [Target]
USING #parameters AS [Source]
ON (
[Target].col1 = [Source].col1
AND [Target].col2 = [Source].col2
AND [Target].col3 = [Source].col3
AND [Target].col4 = [Source].col4
)
WHEN NOT MATCHED BY TARGET
THEN INSERT (col1,col2,col3,col4,col5)
VALUES (
[Source].col1
,[Source].col2
,[Source].col3
,[Source].col4
,[Source].col5
)
WHEN MATCHED
THEN UPDATE SET [Target].col5 = [Source].col5;
i got this error:
for the right syntax to use near 'INNER JOIN oferta B ON A.oferta_id_oferta = B.id_oferta AND B.oferta = "design' at line 4
i can't make a inner join inside a where clause ? or exists other problem with this query ?
UPDATE `oferta_has_tags` A
SET fraccao = "1/7"
WHERE (
INNER JOIN oferta B
ON A.oferta_id_oferta = B.id_oferta
AND B.oferta = "designer"
AND B.estado = 0)
Express it as a simple IN:
UPDATE oferta_has_tags
SET fraccao = '1/7'
WHERE oferta_id_oferta IN (
SELECT id_oferta
FROM oferta
WHERE oferta = 'designer'
AND estado = 0)
Also, changed double quotes (") to single quotes (') - using double quotes will cause an error
The query is wrong. It must have SELECT and FROM clauses:
It must be something like this:
UPDATE oferta_has_tags A
SET fraccao = "1/7"
WHERE id = ( SELECT id FROM yourtable WHERE something = somevalue )
Make sure that the subquery should return exactly 1 value. If you want to update multiple records using above query, replace "=" with "IN". Like this:
UPDATE oferta_has_tags A
SET fraccao = "1/7"
WHERE id IN ( SELECT id FROM yourtable WHERE something = somevalue )
Hope it helps...