<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Query somma campo con lo stsso idUtente]]></title><description><![CDATA[<p dir="auto">Salve a tutti, premetto che ho una conoscenza base sia di mysql che ti php.<br />
Arrivo al punto, ho diverse query da mettere insieme, ma non riesco ad unirle e visualizzare i risultati che mi servono.<br />
Iniziamo con una semplice...<br />
Ho una tabella con questi campi:<br />
id<br />
idCitta<br />
nomeCitta<br />
abitantiCitta<br />
idUtente<br />
nomeUtente</p>
<p dir="auto">In output vorrei avere una tabella con un campo in più, chiamiamolo abitantiUtente, che sarebbe la somma di abitantiCitta corrispondente allo stesso utente.<br />
Ad esempio la tabella originale è questa:</p>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>id</th>
<th>idCitta</th>
<th>nomeCitta</th>
<th>abitantiCitta</th>
<th>idUtente</th>
<th>nomeUtente</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1200</td>
<td>Metropolis</td>
<td>200</td>
<td>10</td>
<td>Filippo</td>
</tr>
<tr>
<td>2</td>
<td>1300</td>
<td>Roma</td>
<td>120</td>
<td>19</td>
<td>Giovanni</td>
</tr>
<tr>
<td>3</td>
<td>1400</td>
<td>Lugano</td>
<td>87</td>
<td>19</td>
<td>Giovanni</td>
</tr>
<tr>
<td>4</td>
<td>1500</td>
<td>Smallville</td>
<td>100</td>
<td>10</td>
<td>Filippo</td>
</tr>
</tbody>
</table>
<p dir="auto">E in output vorrei avere questa:</p>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>id</th>
<th>idCitta</th>
<th>nomeCitta</th>
<th>abitantiCitta</th>
<th>idUtente</th>
<th>nomeUtente</th>
<th>abitantiUtente</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1200</td>
<td>Metropolis</td>
<td>200</td>
<td>10</td>
<td>Filippo</td>
<td>300</td>
</tr>
<tr>
<td>2</td>
<td>1300</td>
<td>Roma</td>
<td>120</td>
<td>19</td>
<td>Giovanni</td>
<td>207</td>
</tr>
<tr>
<td>3</td>
<td>1400</td>
<td>Lugano</td>
<td>87</td>
<td>19</td>
<td>Giovanni</td>
<td>207</td>
</tr>
<tr>
<td>4</td>
<td>1500</td>
<td>Smallville</td>
<td>100</td>
<td>10</td>
<td>Filippo</td>
<td>300</td>
</tr>
</tbody>
</table>
<p dir="auto">Grazie per le risposte.</p>
]]></description><link>https://dev.connect.gt/topic/177836/query-somma-campo-con-lo-stsso-idutente</link><generator>RSS for Node</generator><lastBuildDate>Wed, 25 Nov 2026 21:33:54 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/177836.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 02 Oct 2012 09:59:46 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Query somma campo con lo stsso idUtente on Thu, 04 Oct 2012 06:54:59 GMT]]></title><description><![CDATA[<p dir="auto">Bene, in ogni caso, sono sempre disponibile ad aiutarti per qualsiasi problema.</p>
<p dir="auto">Marco</p>
]]></description><link>https://dev.connect.gt/post/1089076</link><guid isPermaLink="true">https://dev.connect.gt/post/1089076</guid><dc:creator><![CDATA[marcopuccetti]]></dc:creator><pubDate>Thu, 04 Oct 2012 06:54:59 GMT</pubDate></item><item><title><![CDATA[Reply to Query somma campo con lo stsso idUtente on Wed, 03 Oct 2012 21:39:59 GMT]]></title><description><![CDATA[<p dir="auto">Perfetto! Funziona alla grande! Non avevo letto bene il post precedente, mea culpa...<br />
Ecco, diciamo che quei risultati non mi serve averli in output, la sottoquery è un condizione della query principale e mi serviva ottenere quel risultato....comunque, avremo modo di parlarne, ho da ottimizzare un paio di query e se non riesco a risolvere apro un altro post.<br />
Grazie mille <img src="https://dev.connect.gt/assets/plugins/nodebb-plugin-emoji/emoji/android/1f609.png?v=12b9555a9a1" class="not-responsive emoji emoji-android emoji--wink" style="height:1.4em;width:auto;vertical-align:middle" title=";)" alt="😉" /></p>
]]></description><link>https://dev.connect.gt/post/1089083</link><guid isPermaLink="true">https://dev.connect.gt/post/1089083</guid><dc:creator><![CDATA[peppeocchi]]></dc:creator><pubDate>Wed, 03 Oct 2012 21:39:59 GMT</pubDate></item><item><title><![CDATA[Reply to Query somma campo con lo stsso idUtente on Wed, 03 Oct 2012 10:35:14 GMT]]></title><description><![CDATA[<p dir="auto">Prova così:</p>
<p dir="auto"><strong>SELECT DISTINCT <a href="http://T1.id" rel="nofollow ugc">T1.id</a>, T1.idCitta, <strong>T1.<strong>nomeCitta, <strong>T1.<strong>abitantiCitta, <strong>T1.<strong>idUtente, <strong>T1.<strong>nomeUtente,</strong></strong></strong></strong></strong></strong></strong></strong></strong><br />
<strong><strong><strong><strong><strong><strong><strong><strong><strong>(</strong></strong></strong></strong></strong></strong></strong></strong></strong><br />
<strong><strong><strong><strong><strong><strong><strong><strong><strong>SELECT SUM(abitantiCitta) as abitantiUtente FROM TABELLA T2 WHERE</strong></strong></strong></strong></strong></strong></strong></strong></strong> **T2.idUtente = T1.**<strong>idUtente</strong><br />
<strong><strong><strong><strong><strong><strong><strong><strong><strong>)</strong></strong></strong></strong></strong></strong></strong></strong></strong><br />
<strong><strong><strong><strong><strong><strong><strong><strong><strong>FROM</strong></strong></strong></strong></strong></strong></strong></strong></strong> <strong><strong><strong><strong><strong><strong><strong><strong><strong>TABELLA</strong></strong></strong></strong></strong></strong></strong></strong></strong> <strong><strong><strong><strong><strong><strong><strong><strong><strong>T1</strong></strong></strong></strong></strong></strong></strong></strong></strong><br />
<strong><strong><strong><strong><strong><strong><strong><strong><strong>ORDER BY</strong></strong></strong></strong></strong></strong></strong></strong></strong> <strong><strong><strong><strong><strong><strong>T1.<strong>idUtente</strong></strong></strong></strong></strong></strong></strong><br />
<strong><strong><strong><strong><strong><strong><strong><strong><strong>)</strong></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p dir="auto"><strong><strong><strong><strong><strong><strong><strong><strong><strong>L'alias nella sotto-select dovrebbe garantirti che ad ogni passo venga eseguita la somma solo sugli</strong></strong></strong></strong></strong></strong></strong></strong></strong> <em><strong>idUtente</strong></em> <strong><strong><strong><strong><strong><strong><strong><strong><strong>espressi nella condizione</strong></strong></strong></strong></strong></strong></strong></strong></strong> <em><strong>T2.idUtente = T1.<strong>idUtente</strong></strong></em>******************.******************</p>
<p dir="auto"><strong><strong><strong><strong><strong><strong><strong><strong><strong>Praticamente con l'alias utilizzi una tabella in due contesti differenti (gli alias sono T1 e T2).</strong></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p dir="auto"><strong><strong><strong><strong><strong><strong><strong><strong><strong>Provala e fammi sapere</strong></strong></strong></strong></strong></strong></strong></strong></strong><br />
<strong><strong><strong><strong><strong><strong><strong><strong><strong>Marco</strong></strong></strong></strong></strong></strong></strong></strong></strong></p>
]]></description><link>https://dev.connect.gt/post/1089075</link><guid isPermaLink="true">https://dev.connect.gt/post/1089075</guid><dc:creator><![CDATA[marcopuccetti]]></dc:creator><pubDate>Wed, 03 Oct 2012 10:35:14 GMT</pubDate></item><item><title><![CDATA[Reply to Query somma campo con lo stsso idUtente on Wed, 03 Oct 2012 10:20:57 GMT]]></title><description><![CDATA[<p dir="auto">Considera che io ho una sola tabella, quindi ho modificato quello che hai scritto considerando in entrambe le select la tabella T1.<br />
Il risultato è che in ouput nella nuova colonna mi somma tutti gli abitanti di ogni citta della tabella (non solo quelli corrispondenti ad ogni idUtente).<br />
Mi sfugge qualcosa, ma non capisco cosa..</p>
]]></description><link>https://dev.connect.gt/post/1089081</link><guid isPermaLink="true">https://dev.connect.gt/post/1089081</guid><dc:creator><![CDATA[peppeocchi]]></dc:creator><pubDate>Wed, 03 Oct 2012 10:20:57 GMT</pubDate></item><item><title><![CDATA[Reply to Query somma campo con lo stsso idUtente on Tue, 02 Oct 2012 13:10:43 GMT]]></title><description><![CDATA[<p dir="auto">Ciao, prova ad utilizzare gli alias come ho riportato di seguito.<br />
In questo modo non dovrebbe servire neppure il raggruppamento.<br />
Per l'ordinamento fai come meglio desideri.<br />
Considera che adesso non posso provarla, per cui dammi un riscontro sull'esito del test.</p>
<p dir="auto"><strong>SELECT <a href="http://T1.id" rel="nofollow ugc">T1.id</a>, T1.idCitta,</strong>  **T1.**<strong>nomeCitta,</strong>  **T1.**<strong>abitantiCitta,</strong>  **T1.**<strong>idUtente,</strong>  **T1.**<strong>nomeUtente,</strong><br />
<strong>(</strong><br />
<strong>SELECT SUM(abitantiCitta) as abitantiUtente FROM TABELLA T2 WHERE T2.idUtente = T1.IdUser</strong><br />
<strong>)</strong><br />
<strong>FROM TABLE T1</strong><br />
<strong>)</strong></p>
<p dir="auto">Marco Puccetti</p>
]]></description><link>https://dev.connect.gt/post/1089073</link><guid isPermaLink="true">https://dev.connect.gt/post/1089073</guid><dc:creator><![CDATA[marcopuccetti]]></dc:creator><pubDate>Tue, 02 Oct 2012 13:10:43 GMT</pubDate></item><item><title><![CDATA[Reply to Query somma campo con lo stsso idUtente on Tue, 02 Oct 2012 11:34:40 GMT]]></title><description><![CDATA[<p dir="auto">Ciao e grazie per la risposta.<br />
Quello che hai appena scritto io lo faccio già, cioè recuperarmi la somma degli abitanti delle città con group by idUtente e mandarla in output a parte (quindi con due SELECT diverse)<br />
Quello che vorrei è eliminare la select che calcola gli abitantiUtente e accorparla a quella principale (che prende tutti i record della tabella).<br />
Diciamo che ho pensato ad una soluzione, che ovviamente non funziona, magari potreste darmi un mano a sistemarla</p>
<pre><code>SELECT *, (
SELECT SUM(table.abitantiCitta)
FROM table
GROUP BY idUtente
) AS abitantiUtente
FROM table
</code></pre>
<p dir="auto">Ovviamente questa query non funziona....</p>
]]></description><link>https://dev.connect.gt/post/1089080</link><guid isPermaLink="true">https://dev.connect.gt/post/1089080</guid><dc:creator><![CDATA[peppeocchi]]></dc:creator><pubDate>Tue, 02 Oct 2012 11:34:40 GMT</pubDate></item><item><title><![CDATA[Reply to Query somma campo con lo stsso idUtente on Tue, 02 Oct 2012 10:53:44 GMT]]></title><description><![CDATA[<p dir="auto">Ciao, con una semplice group by puoi ricavare la somma degli abitanti per ogni idUtente, con un altra query recuperi la lista dei campi che ti servono, dopodichè crei la stringa di output in formato HTML (ho ipotizzato che il dato debba essere visualizzato su una pagina web).<br />
Trovi la sintassi sul sito w3cschools.</p>
<p dir="auto">Marco Puccetti</p>
]]></description><link>https://dev.connect.gt/post/1089072</link><guid isPermaLink="true">https://dev.connect.gt/post/1089072</guid><dc:creator><![CDATA[marcopuccetti]]></dc:creator><pubDate>Tue, 02 Oct 2012 10:53:44 GMT</pubDate></item></channel></rss>