I want to achieve table output in HTML for the contents of the SELECT result from DB (MSSQL) and the number of columns that are not expected.

Asked 2 years ago, Updated 2 years ago, 69 views

The server-side language is PHP 7.0.
In general, I couldn't imagine what kind of solution I should take when I accomplished the subject matter, and I asked an unreasonable question.
However, after receiving comments from everyone, I can see some points that I don't understand.

Prerequisites

The content obtained from SELECT is never limited to output from a single table, not all items in the table to be referenced, the function appropriately complements the items, and the number of columns cannot be predicted in the form of a row of a table.

What I don't know

If the columns are variable, the number of items and the name of the items to be retrieved from SQLServer should not be known, and if these are not captured, PHP might not be able to expand them to the table.
However, if I think about it carefully, it is clear that the output is fixed, and the number of items can be taken by COUNT or LENGTH, right?I feel like I can make it happen.

Enter a description of the image here

php database

2022-09-30 20:13

1 Answers

"When I looked at the ""additional note"" part, I was asked to express the ""row owner"" data (sales performance table by employee)" as the ""row owner"" data."


If you are referring to the
row ->row ->row ->row ->row -> GROUP BY and CASE.(Same employee, change to SUM instead of MAX if the date has more than one track record)

Wouldn't it be possible to resolve SQL statements if they could dynamically create as many columns as needed (for example, PHP creates SQL statements with string combinations)?

SELECT
  e.name
  , MAX (CASE WHENs.sales_date='2018-07-05'THENs.amount ELSE NULL END) AS'2018-07-05'
  , MAX (CASE WHENs.sales_date='2018-07-06'THENs.amount ELSE NULL END) AS'2018-07-06'
  , MAX (CASE WHENs.sales_date='2018-07-07'THENs.amount ELSE NULL END) AS'2018-07-07'
  , MAX (CASE WHENs.sales_date='2018-07-08'THENs.amount ELSE NULL END) AS'2018-07-08'
  , MAX (CASE WHENs.sales_date='2018-07-09'THENs.amount ELSE NULL END) AS'2018-07-09'
  , MAX (CASE WHENs.sales_date='2018-07-10'THENs.amount ELSE NULL END) AS'2018-07-10'
  , MAX (CASE WHENs.sales_date='2018-07-11'THENs.amount ELSE NULL END) AS'2018-07-11'
from
  sales ASs
JOIN
  employees ASe
ON
s.employee_id=e.id
GROUP BY
  s.employee_id
ORDER BY e.id


2022-09-30 20:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.