mysql content as column names - mysql

I have a table with the contents and columns like in the picture (left side), but I want to display it on my website page with format like in picture (right).
I've tried various combinations of php and mysql syntax but still can not display them.
SELECT DISTINCT
'NAMA',
'TEMPAT LAHIR',
COUNT(PENDIDIKAN) AS Jumlah
FROM
table_name
GROUP BY
'TEMPAT LAHIR'
ORDER BY
NAMA,
'TEMPAT LAHIR'

Try following.
SELECT `TEMPAT LAHIR`,
COUNT(CASE WHEN (PENDIDIKAN = 'S1') THEN PENDIDIKAN ELSE NULL END) AS S1,
COUNT(CASE WHEN (PENDIDIKAN = 'S2') THEN PENDIDIKAN ELSE NULL END) AS S2,
COUNT(CASE WHEN (PENDIDIKAN = 'SMA') THEN PENDIDIKAN ELSE NULL END) AS SMA
FROM `table_name`
GROUP BY `TEMPAT LAHIR`

You don't need 'NAMA' in your query, but you want the 'Jumlah' column. Like this:
SELECT DISTINCT
`TEMPAT LAHIR`,
COUNT(`PENDIDIKAN`) AS `JumlahCount`,
`Jumlah`
FROM
`table_name`
GROUP BY
`TEMPAT LAHIR`
ORDER BY
'TEMPAT LAHIR`
After that a bit of PHP is needed to render it correctly. I know this answer is incomplete, but the same could be said of your question. It was just too long to put in a comment.

Related

How to pivot table in MySql?

I have a table illustrated on the attached screen:
And data is presented:
As you can see it contains all fields from lines (value, column_id - is field type). Each field has line_index and column_index of position and column_id.
I want to get back all lines with all field names like:
line_index column_id_1 column_name1 column_id_2 column_name2 column_id_3 column_name3
0 1 Age 2 Vasile 3 NY
More simply I need to build all fields(rows) to columns then to lines back.
Should I use Pivot and is it possible in MySQL?
Link to sqlfiddle
You will need to group the results by line_index and conditionally transform rows to columns.
SELECT l.line_index,
MAX(CASE WHEN ct.column_name = "NAME" THEN l.value ELSE NULL END) AS "NAME" ,
MAX(CASE WHEN ct.column_name = "AGE" THEN l.value ELSE NULL END) AS "AGE" ,
MAX(CASE WHEN ct.column_name = "ZUP" THEN l.value ELSE NULL END) AS "ZUP"
FROM columns_types ct
LEFT JOIN `lines` l ON l.column_id = ct.column_id
GROUP BY l.line_index;
Try it here. Also I did some changes in your schema which I felt didn't impact the data stored in the tables. You were adding redundant rows(perhaps) in your columns_types table. And by looking at the query, you know that you will have to build a MAX(CASE statement for each column, so its best if you first fetch it and then build the final query in some programming language.

How to display duplicate count from two tables where one column duplicate value separate in two column from each table

First of all I want to thank you all members of this community for posting very helpful answers. I am using this kind of plateform first so I am sorry for my grammatical mistakes and if it is a silly question.
I am having a use case as given in below image link.
Having two tables 1. is Office_1 and 2. is Office_2 and want the Desired Output
For this use case I am able to get Output separately only from one table using below query
First query is for one table (Office_1)
select EID as EID,sum(case when OP like 'come' then 1 else 0 end) as 'Office_1 Come COUNT',sum(case when OP like 'go' then 1 else 0 end) as 'Office_1 Go COUNT' from Office_1 where DATE >= '2022-01-16' AND DATE <= '2022-01-18' group by UID;
Second query is for another table (Office_2)
select EID as EID,sum(case when OP like 'come' then 1 else 0 end) as 'Office_2 Come COUNT',sum(case when OP like 'go' then 1 else 0 end) as 'Office_2 Go COUNT' from Office_2 where DATE >= '2022-01-16' AND DATE <= '2022-01-18' group by UID;
But I want that UID must be selected from first table and output must be combined to display. Please help me in this use case. How to get the desired output?
select* from(select EID as EID,sum(case when OP like 'come' then 1 else 0 end) as 'Office_1 Come COUNT',sum(case when OP like 'go' then 1 else 0 end) as 'Office_1 Go COUNT',date as dt from Office_1
union
select EID as EID,sum(case when OP like 'come' then 1 else 0 end) as 'Office_2 Come COUNT',sum(case when OP like 'go' then 1 else 0 end) as 'Office_2 Go COUNT',date as dt from Office_2)
where dt >= '2022-01-16' AND DATE <= '2022-01-18' group by UID;
Please refer here for more information about union select https://dev.mysql.com/doc/refman/8.0/en/union.html

SELECT CASE, COUNT(*)

I want to select the number of users that has marked some content as favorite and also return if the current user has "voted" or not. My table looks like this
CREATE TABLE IF NOT EXISTS `favorites` (
`user` int(11) NOT NULL DEFAULT '0',
`content` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`user`,`content`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
Say I have 3 rows containing
INSERT INTO `favorites` (`user`, `content`) VALUES
(11, 26977),
(22, 26977),
(33, 26977);
Using this
SELECT COUNT(*), CASE
WHEN user='22'
THEN 1
ELSE 0
END as has_voted
FROM favorites WHERE content = '26977'
I expect to get has_voted=1 and COUNT(*)=3 but
I get has_voted=0 and COUNT(*)=3. Why is that? How to fix it?
This is because you mixed aggregated and non-aggregated expressions in a single SELECT. Aggregated expressions work on many rows; non-aggregated expressions work on a single row. An aggregated (i.e. COUNT(*)) and a non-aggregated (i.e. CASE) expressions should appear in the same SELECT when you have a GROUP BY, which does not make sense in your situation.
You can fix your query by aggregating the second expression - i.e. adding a SUM around it, like this:
SELECT
COUNT(*) AS FavoriteCount
, SUM(CASE WHEN user=22 THEN 1 ELSE 0 END) as has_voted
FROM favorites
WHERE content = 26977
Now both expressions are aggregated, so you should get the expected results.
Try this with SUM() and without CASE
SELECT
COUNT(*),
SUM(USER = '22') AS has_voted
FROM
favorites
WHERE content = '26977'
See Fiddle Demo
Try this:
SELECT COUNT(*), MAX(USER=22) AS has_voted
FROM favorites
WHERE content = 26977;
Check the SQL FIDDLE DEMO
OUTPUT
| COUNT(*) | HAS_VOTED |
|----------|-----------|
| 3 | 1 |
You need sum of votes.
SELECT COUNT(*), SUM(CASE
WHEN user='22'
THEN 1
ELSE 0
END) as has_voted
FROM favorites WHERE content = '26977'
You are inadvertently using a MySQL feature here: You aggregate your results to get only one result record showing the number of matches (aggregate function COUNT). But you also show the user (or rather an expression built on it) in your result line (without any aggregate function). So the question is: Which user? Another dbms would have given you an error, asking you to either state the user in a GROUP BY or aggregate users. MySQL instead picks a random user.
What you want to do here is aggregate users (or rather have your expression aggregated). Use SUM to sum all votes the user has given on the requested content:
SELECT
COUNT(*),
SUM(CASE WHEN user='22' THEN 1 ELSE 0 END) as sum_votes
FROM favorites
WHERE content = '26977';
You forgot to wrap the CASE statement inside an aggregate function. In this case has_voted will contain unexpected results since you are actually doing a "partial group by". Here is what you need to do:
SELECT COUNT(*), SUM(CASE WHEN USER = 22 THEN 1 ELSE 0 END) AS has_voted
FROM favorites
WHERE content = 26977
Or:
SELECT COUNT(*), COUNT(CASE WHEN USER = 22 THEN 1 ELSE NULL END) AS has_voted
FROM favorites
WHERE content = 26977

MYSQL get count of each column where it equals a specific value

I recently set up a MYSQL database connected to a form filled with checkboxes. If the checkbox was selected, it would insert into the associated column a value of '1'; otherwise, it would receive a value of '0'.
I'd like to eventually look at aggregate data from this form, and was wondering if there was any way I could use MYSQL to get a number for each column which would be equal to the number of rows that had a value of '1'.
I've tried variations of:
select count(*) from POLLDATA group by column_name
which was unsuccessful, and nothing else I can think of seems to make sense (admittedly, I'm not all too experienced in SQL).
I'd really like to avoid:
select count(*) from POLLDATA where column_1='1'
for each column (there close to 100 of them).
Is there any way to do this besides typing out a select count(*) statement for each column?
EDIT:
If it helps, the columns are 'artist1', 'artist2', ....'artist88', 'gender', 'age', 'city', 'state'. As I tried to explain below, I was hoping that I'd be able to do something like:
select sum(EACH_COLUMN) from POLLDATA where gender='Male', city='New York City';
(obviously EACH_COLUMN is bogus)
SELECT SUM(CASE
WHEN t.your_column = '1' THEN 1
ELSE 0
END) AS OneCount,
SUM(CASE
WHEN t.your_column='0' THEN 1
ELSE 0
END) AS ZeroCount
FROM YOUR_TABLE t
If you are just looking for the sheer number of 1's in the columns, you could try…
select sum(col1), sum(col2), sum(col3) from POLLDATA
A slightly more compact notation is SUM( IF( expression ) ).
For the askers example, this could look something like:
select
count(*) as total,
sum(if(gender = 'MALE', 1, 0)) as males,
sum(if(gender = 'FEMALE', 1, 0)) as females,
sum(if(city = 'New York City', 1, 0)) as newYorkResidents
from POLLDATA;
Example result:
+-------+-------+---------+------------------+
| total | males | females | newYorkResidents |
+-------+-------+---------+------------------+
| 42 | 23 | 19 | 42 |
+-------+-------+---------+------------------+
select count(*) from POLLDATA group by column_name
I dont think you want to do a count cause this will also count the records with a 0.
try
select column_naam,sum(column_name) from POLLDATA group by column_name
or
select column_naam,count(*) from POLLDATA
where column_name <> 0
group by column_name
only adds the 0
Instead of strings why not store actual numbers, 1 or 0.
Then you could use the sql SUM function.
When the query begins to be a little too complicated, maybe it's because you should think again about your database structure. But if you want to keep your table as it is, you could use a prepared statement that automatically calculates all the sums for you, without specifying every single column:
SELECT
CONCAT(
'SELECT ',
GROUP_CONCAT(CONCAT('SUM(', `column_name`, ') AS sum_', `column_name`)),
' FROM POLLDATA WHERE gender=? AND city=?')
FROM `information_schema`.`columns`
WHERE `table_schema`=DATABASE()
AND `table_name`='POLLDATA'
AND `column_name` LIKE 'artist%'
INTO #sql;
SET #gender := 'male';
SET #city := 'New York';
PREPARE stmt FROM #sql;
EXECUTE stmt USING #gender, #city;
Please see fiddle here.

MAX with extra criteria

I have the following part of a query I'm working on in MYSQL.
SELECT
MAX(CAST(MatchPlayerBatting.BatRuns AS SIGNED)) AS HighestScore
FROM
MatchPlayerBatting
It returns the correct result. However there is another column I need it to work off.
That is if the maximum value it finds also has a value of "not out" within "BatHowOut", it should show the result as for example 96* rather than just 96.
How could this be done?
To help make the data concrete, consider two cases:
BatRuns BatHowOut
96 not out
96 lbw
BatRuns BatHowOut
96 not out
102 lbw
For the first data, the answer should be '96*'; for the second, '102'.
You can achieve this using self-join like this:
SELECT t1.ID
, CONCAT(t1.BatRuns,
CASE WHEN t1.BatHowOut = 'Not Out' THEN '*' ELSE '' END
) AS HighScore
FROM MatchPlayerBatting t1
JOIN
(
SELECT MAX(BatRuns) AS HighestScore
FROM MatchPlayerBatting
) t2
ON t1.BatRuns = t2.HighestScore
See this sample SQLFiddle with highest "Not Out"
See this another sample SQLFiddle with highest "Out"
See this another sample SQLFiddle with two highest scores
How about ordering the scores in descending order and selecting only the first record?
select concat(BatRuns , case when BatHowOut = 'not out' then '*' else '' end)
from mytable
order by cast(BatRuns as signed) desc,
(case when BatHowOut = 'not out' then 1 else 2 end)
limit 1;
Sample here.
If you want to find highest score score for each player, here is a solution that may not be elegant, but quite effective.
select PlayerID,
case when runs != round(runs)
then concat(round(runs),'*')
else
round(runs)
end highest_score
from (select PlayerID,
max(cast(BatRuns as decimal) +
case when BatHowOut = 'not out' then 0.1 else 0 end
) runs
from MatchPlayerBatting
group by PlayerID) max_runs;
This takes advantage of the fact that, runs can never be fractions, only whole numbers. When there is a tie for highest score and one of them is unbeaten,
adding 0.1 to the unbeaten score will make it the highest. This can be later removed and concatenated with *.
Sample here.