Advanced SQL query in symfony2 doctrine - mysql

I am working on a project using Symfony2.8 and MySQL.
I have 4 tables in my database that are users, quotes, articles, articlesquotes.
user table
<html>
<body>
<table border="1">
<tr>
<th>user_id</th>
<th>name</th>
<th>age</th>
</tr>
<tr>
<td>7</td>
<td>Alex</td>
<td>20</td>
</tr>
<tr>
<td>8</td>
<td>John</td>
<td>30</td>
</tr>
</table>
</body>
</html>
quotes table
<html>
<body>
<table border="1">
<tr>
<th>quote_id</th>
<th>user_id</th>
<th>reference</th>
</tr>
<tr>
<td>61</td>
<td>7</td>
<td>AE20</td>
</tr>
<tr>
<td>62</td>
<td>7</td>
<td>AE21</td>
</tr>
<tr>
<td>63</td>
<td>7</td>
<td>AE22</td>
</tr>
<tr>
<td>64</td>
<td>8</td>
<td>AE29</td>
</tr>
</table>
</body>
</html>
articlesquote
<html>
<body>
<table border="1">
<tr>
<th>id</th>
<th>quote_id</th>
<th>article_id</th>
<th>qte</th>
</tr>
<tr>
<td>58</td>
<td>61</td>
<td>2</td>
<td>7</td>
</tr>
<tr>
<td>59</td>
<td>62</td>
<td>3</td>
<td>8</td>
</tr>
<tr>
<td>60</td>
<td>63</td>
<td>1</td>
<td>9</td>
</tr>
<tr>
<td>61</td>
<td>63</td>
<td>2</td>
<td>10</td>
</tr>
</table>
</body>
</html>
articles table
<html>
<body>
<table border="1">
<tr>
<th>article_id</th>
<th>name</th>
</tr>
<tr>
<td>1</td>
<td>article1</td>
</tr>
<tr>
<td>2</td>
<td>article2</td>
</tr>
<tr>
<td>3</td>
<td>article3</td>
</tr>
</table>
</body>
</html>
now the relation between these tables is
user<--One-To-Many-->quote<--one-To-Many-->ArticlesQuote<--Many-To-One-->Article
I would like to get articles that are in all quotes of user number 7 ordered by quote_id
I would like to get articles that are in quote number 63 of the user number 7
thank you for helping me.

try this:
Select a.* From articles a
Inner Join
articlesquote aq On a.article_id=aq.article_id
Inner Join
quotes q On q.quote_id=aq.quote_id
Inner Join
user u On u.user_id=q.user_id
Where
u.user_id=7 and q.quote_id=63

Related

Merge two rows in single to in SQL Server

I have data like this:
<table>
<tr>
<th>Item_Code</th>
<th>Size</th>
<th>Count</th>
</tr>
<tr>
<td>1001</td>
<td>XL</td>
<td>10</td>
</tr>
<tr>
<td>1001</td>
<td>XXL</td>
<td>5</td>
</tr>
<tr>
<td>1002</td>
<td>3XL</td>
<td>3</td>
</tr>
<tr>
<td>1002</td>
<td>XL</td>
<td>10</td>
</tr>
</table
I want result as below
<table>
<tr>
<th>Item_Code</th>
<th>Size</th>
<th>Count</th>
</tr>
<tr>
<td>1001</td>
<td>XL,XXL</td>
<td>15</td>
</tr>
<tr>
<td>1002</td>
<td>3XL,XL</td>
<td>13</td>
</tr>
</table>
I want this thing in SQL Server. Can anyone please help me with this?
I know the query to concatenate the multiple row values into a single column, but I don't know along with other columns also.

Get Average and Join 3 tables

I have 3 tables which are given below;
User table:
<table border="1">
<tr>
<td>uid</td>
<td>name</td>
<td>status</td>
<td>category</td>
</tr>
<tr>
<td>1</td>
<td>John Doe</td>
<td>1</td>
<td>C</td>
</tr>
<tr>
<td>2</td>
<td>Jane Doe</td>
<td>1</td>
<td>C</td>
</tr>
</table>
Profile Table:
<table border="1">
<tr>
<td>pid</td>
<td>uid</td>
<td>slug</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>john-doe</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>jane-doe</td>
</tr>
</table>
And Rating Table:
<table border="1">
<tr>
<td>rid</td>
<td>uid</td>
<td>rating</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>3.5</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>2.5</td>
</tr>
<tr>
<td>3</td>
<td>2</td>
<td>3.5</td>
</tr>
<tr>
<td>4</td>
<td>1</td>
<td>2.5</td>
</tr>
<tr>
<td>5</td>
<td>2</td>
<td>3.5</td>
</tr>
<tr>
<td>6</td>
<td>2</td>
<td>2.5</td>
</tr>
<tr>
<td>7</td>
<td>1</td>
<td>3.5</td>
</tr>
<tr>
<td>8</td>
<td>2</td>
<td>2.5</td>
</tr>
</table>
I m trying to join the table and get value having common id with avg rating from rating fromr rating table.
the output I want is given below;
<table border="1">
<tr>
<td>uid</td>
<td>name</td>
<td>slug</td>
<td>status</td>
<td>category</td>
<td>rating</td>
</tr>
<tr>
<td>1</td>
<td>John Doe</td>
<td>john-doe</td>
<td>1</td>
<td>C</td>
<td>3.25</td>
</tr>
<tr>
<td>2</td>
<td>Jane Doe</td>
<td>jane-doe</td>
<td>1</td>
<td>C</td>
<td>3</td>
</tr>
</table>
Please try below query, I think this will work for you.
select u.*, p.slug, (select avg(rating) from Rating r where r.uid=u.uid) as rating from User u JOIN Profile p ON p.uid=u.uid

How to arrange table td using rowspan

I have to arrange table cells as shown in the image below, I am using bootstrap, and I have adjusted it using colspan. but it not works as like in the image.
I have fiddeled here
<table class="table table-bordered table-responsive">
<thead></thead>
<tfoot></tfoot>
<tbody>
<tr>
<td rowspan="4">a</td>
<td rowspan="2" colspan="2">aaa</td>
</tr>
<tr>
<td>asas</td>
<td>asas</td>
</tr>
<tr>
<td>asas</td>
<td>asas</td>
</tr>
<tr>
<td>asas</td>
<td>asas</td>
</tr>
</tbody>
How can I get the table as shown in the image.
I use this to generate the table:
<table border="1" width="500">
<tr>
<td rowspan="3">1</td>
<td colspan="2">2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td rowspan="4">7</td>
<td>8</td>
<td>9</td>
</tr>
<tr>
<td>10</td>
<td>11</td>
</tr>
<tr>
<td>12</td>
<td>13</td>
</tr>
<tr>
<td>14</td>
<td>15</td>
</tr>
</table>

How do I create an HTML table with the following structure?

I'm trying to create a table with the following structure.
I've been reading various sites and blogs to try to create this myself, but I have failed, terribly, and decided to ask for help here.
So far, I've been able to create the outer structure:
<table border='1' style="width:100%">
<tr>
<td>Foo</td>
<td>Bar</td>
</tr>
<tr>
<td>Bak</td>
<tr></tr>
<td>Baz</td>
</tr>
<tr>
<td>Foo</td>
<td>Bar</td>
</tr>
<tr>
<td>Bak</td>
<td>Baz</td>
</tr>
</table>
But I can't figure out how to add the fields for Name, Number, and Cost. How do I nest it?
Use rowspan to cause a cell to appear in multiple rows, headers to link your data cells with header cells that aren't in traditional positions, and tbody to describe subdivisions of a table.
table, td, th {
border-collapse: collapse;
border: solid black 1px;
padding: 3px;
}
<table>
<tbody>
<tr>
<td rowspan="3"> ...
<th id="name_1"> Name
<td headers="name_1"> ...
<tr>
<th id="number_1"> Number
<td headers="number_1"> ...
<tr>
<th id="cost_1"> Cost
<td headers="cost_1"> ...
<tbody>
<tr>
<td rowspan="3"> ...
<th id="name_2"> Name
<td headers="name_2"> ...
<tr>
<th id="number_2"> Number
<td headers="number_2"> ...
<tr>
<th id="cost_2"> Cost
<td headers="cost_2"> ...
</table>
You need to use rowspan JSFIDDLE
<table border="1">
<tr>
<td rowspan="3"></td>
<td>Name</td>
<td>&nbsp</td>
</tr>
<tr>
<td>Number</td>
<td> </td>
</tr>
<tr>
<td>cost</td>
<td> </td>
</tr>
<tr>
<td rowspan="3"></td>
<td>Name</td>
<td>&nbsp</td>
</tr>
<tr>
<td>Number</td>
<td> </td>
</tr>
<tr>
<td>cost</td>
<td> </td>
</tr>
</table>
try fiddle fiddle
<table border='1' style="width:100%">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td rowspan="3">some</td>
<td>Name</td>
<td>11</td>
</tr>
<tr>
<td>No</td>
<td>22</td>
</tr>
<tr>
<td>Cost</td>
<td>22</td>
</tr>
</table>
User rowspan
<table border="1">
<tr>
<td rowspan="3">Data Section 1</td>
<td>Some Data 1.1</td>
</tr>
<tr>
<td>Some Data 1.2</td>
</tr>
<tr>
<td>Some Data 1.3</td>
</tr>
<tr>
<td rowspan="3">Data Section 2</td>
<td>Some Data 2.1</td>
</tr>
<tr>
<td>Some Data 2.2</td>
</tr>
<tr>
<td>Some Data 2.3</td>
</tr>
</table>
<table border='1' style="width: 100%">
<tr>
<td rowspan="3">1</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td rowspan="3">1</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td rowspan="3">1</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
</tr>
</table>
In general way like this
<table border="1">
<tr>
<td rowspan="3">1</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td rowspan="3">2</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td>11</td>
<td>12</td>
</tr>
<tr>
<td rowspan="3">3</td>
<td>13</td>
<td>14</td>
</tr>
<tr>
<td>15</td>
<td>16</td>
</tr>
<tr>
<td>17</td>
<td>18</td>
</tr>
</table>
http://jsfiddle.net/s5b0c5d9/

Table within a table

I'm writing a code for the following table (I have to use table inside table)
Here is my code :
<html>
<body>
<table>
<table border=1 cellspacing=0 width="250px">
<tr>
<td width=20%>1</td>
<td width=20%>2</td>
<td width=20%>3</td>
<td width=20%>4</td>
<td width=20%>5</td>
</tr>
</table>
<table border=1 cellspacing=0 width="250px">
<tr>
<td width=60%>6</td>
<td width=20%>7</td>
<td width=20%>8</td>
</tr>
</table>
<table border=1 cellspacing=0 width="250px">
<tr>
<td width=20%>9</td>
<td width=20%>10</td>
<td width=20%>11</td>
<td width=20% rowspan=2>12</td>
<td width=20%>13</td>
</tr>
<tr>
<td>14</td>
<td>15</td>
<td>16</td>
<td>17</td>
</tr>
</table>
</table>
</body>
</html>
But in browser I'm getting something different, looking like this :
I'm very beginner with HTML, I'm trying to understand how tables within tables work mostly by doing exercises for that.
This can be accomplished with one table:
HTML
<table border=1 >
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td colspan="3">6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td rowspan=2>12</td>
<td>13</td>
</tr>
<tr>
<td>14</td>
<td>15</td>
<td>16</td>
<td>17</td>
</tr>
</table>
JS Fiddle: http://jsfiddle.net/HcRW3/
your rowspan in the 3rd row can't work as the table has only one row. It doesn't know anything about the outside table.
I really can't understand why you have to go with table in table, but if you must, the outer table can only have 3 rows and the 3rd nested table has two rows.
May well work better for you:
<html>
<body>
<table border="1" cellspacing="0">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td colspan="3">6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td style="border-bottom: none;">12</td>
<td>13</td>
</tr>
<tr>
<td>14</td>
<td>15</td>
<td>16</td>
<td style="border-top: none;"></td>
<td>17</td>
</tr>
</table>
</body>
</html>
This is not an answer, but just an extension to my comment, to explain the result.
table is not a valid child of tr, only th and td are valid as child of tr.
For most browsers the behavior if they detect an invalide child inside of an element is, to move the child after that element. This is repeated until the structure is valid.
Because table is neither a valid child of tr nor of table the innter tables are all moved outside of the surrounding table. The resulting structure the browsers most likely creates out of your original code of the question:
<table>
<tr>
<table border=1 cellspacing=0>
<tr>
<td width=20%>1</td>
<td width=20%>2</td>
<td width=20%>3</td>
<td width=20%>4</td>
<td width=20%>5</td>
</tr>
</table>
</tr>
<tr>
<table border=1 cellspacing=0>
<tr>
<td width=60%>6</td>
<td width=20%>7</td>
<td width=20%>8</td>
</tr>
</table>
</tr>
<tr>
<table border=1 cellspacing=0>
<tr>
<td width=20%>9</td>
<td width=20%>10</td>
<td width=20%>11</td>
<td width=20% rowspan=2>12</td>
<td width=20%>13</td>
</tr>
</table>
</tr>
<tr>
<table border=1 cellspacing=0>
<tr>
<td>14</td>
<td>15</td>
<td>16</td>
<td>17</td>
</tr>
</table>
</tr>
</table>
would then be:
<table>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
</table>
<table border=1 cellspacing=0>
<tr>
<td width=20%>1</td>
<td width=20%>2</td>
<td width=20%>3</td>
<td width=20%>4</td>
<td width=20%>5</td>
</tr>
</table>
<table border=1 cellspacing=0>
<tr>
<td width=60%>6</td>
<td width=20%>7</td>
<td width=20%>8</td>
</tr>
</table>
<table border=1 cellspacing=0>
<tr>
<td width=20%>9</td>
<td width=20%>10</td>
<td width=20%>11</td>
<td width=20% rowspan=2>12</td>
<td width=20%>13</td>
</tr>
</table>
<table border=1 cellspacing=0>
<tr>
<td>14</td>
<td>15</td>
<td>16</td>
<td>17</td>
</tr>
</table>
May I advise you use colspan and Rowspan instead of tables inside tables it much easier and how I do most of my tables which require cells to be merged.
You can find an example of colspan here:
http://www.w3schools.com/tags/att_td_colspan.asp
and an example of rowspan:
http://www.w3schools.com/tags/att_td_rowspan.asp
Here is an example of the code to do this:
<table border="1">
<tr>
<td>1</td>
<td>2</td>
<td >3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td colspan="3">6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td >11</td>
<td rowspan="2">12</td>
<td>13</td>
</tr>
<tr>
<td>14</td>
<td >15</td>
<td >16</td>
<td>17</td>
</tr>
</table>
I'm pretty sure you should be able to do this in one table. Like this,
<table border=1 cellspacing=0>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td colspan=3>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<tdrowspan=2>12</td>
<td>13</td>
</tr>
<tr>
<td>14</td>
<td>15</td>
<td>16</td>
<td>17</td>
</tr>
</table>
You should try not to put a table inside a table. "Rowspan" and "colspan" are a good idea. You'd probably want to use 'valign' top (vertical align) to make sure the 12 stays on the same level as the row.
<table border="1">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td colspan="3">6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td rowspan="2" valign="top">12</td>
<td>13</td>
</tr>
<tr>
<td>14</td>
<td>15</td>
<td>16</td>
<td>17</td>
</tr>
</table>