SQL combine one column with another table and add new column - mysql

Sorry for the unclear title. But i want to make column editor_article which has name of editor. Only article with same value among id_article and parent_id have name of editor_article and editor_article has '0' value if id_section = 29 and if parent_id != 0. Editor_article got from editor column join with t_kolom.id_editor.
tbl_name:t_article
id_section id_article parent_id editor
29 441 0 2
33 1093 18 2
33 18 0 0
29 3144 0 8
30 3136 0 0
31 3130 0 0
31 3140 3130 22
31 3141 3130 335
30 3142 3136 546
tbl_name:t_kolom
id_editor name
1 john
2 gerrard
3 lukas
8 anthony
22 jimmy
335 eric
546 tyas
And the expected output:
id_section id_article parent_id editor editor_article
29 441 0 2 0
33 1093 18 2 0
33 18 0 0 gerrard
29 3144 0 8 0
30 3136 0 0 tyas
31 3130 0 0 jimmy,eric
31 3140 3130 22 0
31 3141 3130 335 0
30 3142 3136 546 0

Related

An sdf file that I produce doesn't get accepted by a converter

I made a program that converts csv files into sdf files. Those files were supposed to go in another converter that turns them into something called a "Nist MS Library". The problem is that my file doesn't get accepted by the converter for "No spectra have been converted" and I don't understand why.
The files seem identical to me and I think I'm missing something about the specific file extension.
I'm really sorry if this doesn't belong here, I will delete the post if this is the case, but I really do not know where to ask.
I tried to make the "mass spectral peaks" integer, floats, delete them and put some values that I knew for sure that were accepted by the Nist converter, but nothing seems to work.
I will put 2 molecules, the first one is mine, the one that doesn't get accepted, the other one is the one that is fine for the program.
Coumarin
No Structure
0 0 0 0 0 0 0 0 0 0 0
> <NAME>
Coumarin
> <INCHIKEY>
> <FORMULA>
> <MW>
> <CASNO>
91645
> <ID>
2
> <COMMENT>
SAFC Cat. n. W526509\nColumn: SLB-5ms part#28471-U; Supelcowax-10 part#24079; Equity-1 part#28046-U;\nwww.sigmaaldrich.com |RI:1438|
> <SYNONYMS>
Coumarin
> <NUM PEAKS>
140
> <MASS SPECTRAL PEAKS>
39 1
39 233
40 38
40 0
40 0
41 0
41 1
41 5
42 2
42 2
43 35
43 0
43 12
44 26
45 20
45 67
46 7
46 4
46 2
46 1
47 0
48 4
49 32
49 3
50 5
50 166
51 183
52 20
53 35
54 7
55 1
56 0
58 0
59 2
59 23
60 6
60 11
61 89
62 213
63 503
64 164
64 13
65 15
65 7
66 9
68 4
71 0
71 1
72 2
73 1
73 19
74 0
74 32
75 39
76 14
77 11
77 1
78 0
78 0
79 0
79 9
79 0
80 2
80 1
81 0
81 0
81 1
82 0
82 0
83 0
83 0
84 6
84 0
85 17
86 2
86 2
87 28
88 7
89 523
90 581
91 25
91 48
92 40
92 36
93 4
93 5
94 1
94 0
97 2
98 6
98 0
99 2
99 0
100 0
100 1
101 5
102 3
103 0
103 0
103 0
104 0
105 0
105 0
106 0
106 0
106 0
107 0
108 0
109 0
110 0
110 0
111 0
111 0
112 0
112 0
113 0
116 0
117 6
117 0
118 1000
119 94
120 34
120 10
121 4
121 0
122 0
122 0
131 0
135 0
145 1
146 0
146 547
147 58
148 5
183 0
246 0
334 0
351 0
359 0
382 0
> <RI value>
1430.1
$$$$
ETHYL HYDROSULFIDE
(C) 2015 John Wiley & Sons, Inc.
CAS rn = 75081, Library ID = 1
3 2 0 0 0 0 0 0 0 0999 V2000
0.0000 0.2061 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7146 -0.2061 0.0000 S 0 0 0 0 0 0 0 0 0 0 0 0
-0.7146 -0.2061 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0 0
1 3 1 0 0 0 0
M END
> <NAME>
Ethyl hydrosulfide
> <SYNONYMS>
Ethanethiol
$:28DNJIEGIFACGWOD-UHFFFAOYSA-N
$:29n=703/0/1 p=620/0/1
> <FORMULA>
C2H6S
> <MW>
62
> <CASNO>
75081
> <ID>
1
> <COMMENT>
WileyID="LM_FFNSC3_1" RI1="703 (SLB-5MS (Hydro))" RI2="392 (SLB-5MS (FAMEs))" RI3="620 (Supelcowax-10 (FAMEs)" RI4="568 (Supelcowax-10 (FAEEs)" Contributor="Prof. L. Mondello (Chromaleont s.r.l./Univ. Messina, Italy)"
> <NUM PEAKS>
21
> <MASS SPECTRAL PEAKS>
44 20
45 235
46 147
47 727
48 20
49 32
50 2
51 2
52 2
53 2
54 2
55 2
56 16
57 84
58 115
59 84
60 16
61 155
62 999
63 36
64 44
$$$$

Mysql find common values for all users

We are developing an local shop reccomendation system and in one of our sql queries we had a problem
We want to fetch the companies which all users in same cluster rated , but if any one of the users in the same group doesnt rated the company we wouldnt want to fetch it
SELECT ml_user_clusters.primaryUser,ml_user_clusters.clusterId,ml_ratings.companyId,ml_ratings.rating,ml_user_labels.groupId FROM ml_user_clusters
LEFT JOIN ml_ratings ON ml_ratings.userId = ml_user_clusters.primaryUser
LEFT JOIN ml_company_user_labels ON ml_company_user_labels.companyId = ml_ratings.companyId
LEFT JOIN ml_user_labels ON ml_user_labels.groupId = ml_company_user_labels.labelId
WHERE ml_user_clusters.clusterId = 0
We've started to add a query like in the below but couldnt able to finish it with proper AND clause
And our data is like in the below: So in the result we would like to have only the companies which has groupId=6 because all users in the same cluster(clusterId=0) rated a company with groupId = 6
primaryUser clusterId companyId rating groupId
497 0 135 5 NULL
498 0 135 10 NULL
79 0 135 12 NULL
501 0 135 10 NULL
79 0 85 14 2
79 0 8 4 5
79 0 98 11 5
79 0 3 5 5
497 0 6 7 6
500 0 6 7 6
499 0 29 7 6
497 0 29 7 6
499 0 77 7 6
500 0 29 7 6
498 0 6 7 6
500 0 77 11 6
500 0 130 3 6
498 0 130 3 6
501 0 77 19 6
499 0 6 7 6
79 0 30 1 7
500 0 30 7 7
79 0 48 7 9
79 0 39 1 13
79 0 48 7 13
499 0 6 7 15
497 0 6 7 15
79 0 8 4 15
500 0 6 7 15
79 0 98 11 15
498 0 6 7 15
79 0 3 5 15
79 0 81 7 15
79 0 3 5 17
79 0 82 7 17
79 0 103 7 17
79 0 118 3 17
79 0 63 3 17
501 0 118 7 17
79 0 82 7 19
79 0 118 3 19
79 0 63 3 19
501 0 118 7 19
79 0 39 1 21
79 0 85 14 23
Expected output must be: (Because all unique users in Cluster=0 has rated a company which has GroupID=6 )
primaryUser clusterId companyId rating groupId
497 0 6 7 6
500 0 6 7 6
499 0 29 7 6
497 0 29 7 6
499 0 77 7 6
500 0 29 7 6
498 0 6 7 6
500 0 77 11 6
500 0 130 3 6
498 0 130 3 6
501 0 77 19 6
499 0 6 7 6
Do you have any idea how we can fix that problem?
Something like this should work,you should build a fiddle for better testing.
Explanation: you count distinct users grouped by group id and compare with the total number of distinct users.If the two match it means all users in that respective groupid have voted.
SELECT ml_user_labels.groupId
FROM ml_user_clusters
LEFT JOIN ml_ratings ON ml_ratings.userId = ml_user_clusters.primaryUser
LEFT JOIN ml_company_user_labels ON ml_company_user_labels.companyId = ml_ratings.companyId
LEFT JOIN ml_user_labels ON ml_user_labels.groupId = ml_company_user_labels.labelId
WHERE ml_user_clusters.clusterId = 0
GROUP BY ml_user_labels.groupId
HAVING COUNT(DISTINCT ml_user_clusters.primaryUser) =
(SELECT COUNT(DISTINCT ml_user_clusters.primaryUser)
FROM ml_user_clusters
LEFT JOIN ml_ratings ON ml_ratings.userId = ml_user_clusters.primaryUser
LEFT JOIN ml_company_user_labels ON ml_company_user_labels.companyId = ml_ratings.companyId
LEFT JOIN ml_user_labels ON ml_user_labels.groupId = ml_company_user_labels.labelId
WHERE ml_user_clusters.clusterId = 0)x

Insert php multiple data into mysql

I have 1000+ customers. I require customer report.
Here debit = potato + onion + ginger. Credit is commission.Balance will be updated every time. It will be balance - debit and balance + credit alternatively.
Grocery data report is as: Data is filled through php form with mysql_fetch_array query. Here few customers are as sample. and few data fields.
id cus_id cus_name potato onion ginger debit credit balance
1 12 munna 10 25 28 63 0 37
2 16 anil 24 56 84 164 0 136
3 34 palash 17 47 51 115 0 85
4 45 dimpy 35 64 39 138 0 112
Table grocery before and after entering new data:
id cus_id cus_name potato onion ginger debit credit balance
1 12 munna 10 25 28 63 0 37
2 16 anil 24 56 84 164 0 136
3 34 palash 17 47 51 115 0 85
4 45 dimpy 35 64 39 138 0 112
5 12 munna 0 0 0 0 6 43
6 16 anil 0 0 0 0 16 152
7 34 palash 0 0 0 0 12 97
8 45 dimpy 0 0 0 0 14 126
My problem is :
I am unable to update balance column, cus_name wise and cus_id wise and insert all data into mysql database. Suggest me with mysql query.

How to scrape using rvest in pages with multiple tables

I'm trying to scrape the data from every table at the hockey-reference awards page. I can scrape the first table for the Hart Memorial Trophy, but when I try the rest of them, I end up with empty vectors. I used Selector Gadget and the rvest package to produce the following code.
library(rvest)
url="https://www.hockey-reference.com/awards/voting-2017.html"
byng<-read_html(url)
byng_node<-html_nodes(byng, "#byng_stats .right , #byng_stats a")
byng_text<-html_text(byng_node)
However, once I run this code, I get no data in the byng variables:
> byng_node
{xml_nodeset (0)}
> byng_text
character(0)
What's happening here? Does selector gadget not work for pages with multiple tables? Does it have nothing to do with that and there's something HTMLy I don't understand? Any help is greatly appreciated!
#neilfws was right: if you look at the source code of the HTML page, you see that all but the first table are commented so rvest thinks they are comments, not part of source code itself. Let's do a dirty hack and remove these characters that are used to comment our precious tables:
library(rvest)
url="https://www.hockey-reference.com/awards/voting-2017.html"
byng<-read_html(url)
# Remove commenting sequences
byng <- gsub("<!--", "", byng)
byng <- gsub("-->", "", byng)
byng<-read_html(byng)
#Get tables as a list of dataframes
tables <- html_table(byng)
# Last table
tables[7]
[[1]]
Scoring Scoring Scoring Scoring Goalie Stats Goalie Stats
1 Place Player Age Tm Pos Votes Vote% 1st 2nd 3rd 4th 5th G A PTS +/- W L
2 1 Connor McDavid 20 EDM C 762 94.07 141 18 3 0 0 30 70 100 27
3 2 Sidney Crosby 29 PIT C 526 64.94 20 142 0 0 0 44 45 89 17
4 3 Nicklas Backstrom 29 WSH C 127 15.68 1 2 116 0 0 23 63 86 17
5 4 Mark Scheifele 23 WPG C 21 2.59 0 0 21 0 0 32 50 82 18
6 5 Auston Matthews 19 TOR C 10 1.23 0 0 10 0 0 40 29 69 2
7 6 Evgeni Malkin 30 PIT C 4 0.49 0 0 4 0 0 33 39 72 18
8 7 John Tavares 26 NYI C 2 0.25 0 0 2 0 0 28 38 66 4
9 8 Jonathan Toews 28 CHI C 1 0.12 0 0 1 0 0 21 37 58 7
10 8 Brad Marchand 28 BOS C 1 0.12 0 0 1 0 0 39 46 85 18
11 8 Ryan Kesler 32 ANA C 1 0.12 0 0 1 0 0 22 36 58 8
12 8 Ryan Getzlaf 31 ANA C 1 0.12 0 0 1 0 0 15 58 73 7

Update specific records based on some group

I am using SQL Server 2008 R2.
I am having a database table that contains some user data as given below :
Id UserId Sys Dia ReadingType DataId IsDeleted
1 10 98 65 last 1390556024216 0
2 10 99 69 average 1390556024216 0
3 10 102 96 last 1390562788540 0
4 10 102 96 average 1390562788540 0
5 11 130 98 last 1390631241547 0
6 11 130 98 average 1390631241547 0
7 2 285 199 first 1390770562374 0
8 2 250 180 last 1390770562374 0
9 2 267 189 average 1390770562374 0
10 1 258 180 first 1391191009457 0
11 1 258 180 last 1391191009457 0
12 1 258 180 average 1391191009457 0
13 1 285 199 additional 1391191009457 0
14 22 110 78 last 1391549208338 0
15 22 123 83 last 1391549208349 0
In this table, there are records that are having the same DataId but different ReadingType.
I want to set IsDeleted=1 for the records having ReadingType='last' and having a record with ReadingType='average' with the same DataId, Sys, Dia and UserId.
So the Desired result shoul be :
Id UserId Sys Dia Reading DataId IsDeleted
1 10 98 65 last 1390556024216 0
2 10 99 69 average 1390556024216 0
3 10 102 96 last 1390562788540 1
4 10 102 96 average 1390562788540 0
5 11 130 98 last 1390631241547 1
6 11 130 98 average 1390631241547 0
7 2 285 199 first 1390770562374 0
8 2 250 180 last 1390770562374 0
9 2 267 189 average 1390770562374 0
10 1 258 180 first 1391191009457 0
11 1 258 180 last 1391191009457 1
12 1 258 180 average 1391191009457 0
13 1 285 199 additional 1391191009457 0
14 22 110 78 last 1391549208338 0
15 22 123 83 last 1391549208349 0
Here the records with Id 3, 5 and 11 should be marked as deleted as they are having same UserId, Sys, Dia, DataId and ReadingType="last" with another record having ReadingType="average" with same other fields.
Can anyone help me how to find out such a records and update them?
Just use UPDATE with EXISTS subquery:
UPDATE T
SET IsDeleted=1
WHERE
ReadingType='last'
AND
EXISTS(SELECT * FROM T as T1
WHERE T1.ReadingType='average'
AND T1.DataId=T.DataId
AND T1.Sys=T.Sys
AND T1.Dia=T.Dia
AND T1.UserId=T.UserId
)
SQLFiddle demo
You Can solve many way but here i am using the sub-query to solve your problem
UPDATE TABLE SET IsDeleted=1
WHERE DataId=(SELECT DataId FROM TABEL WHERE Reading='last')