How to manipulate SQL Query output - mysql

I would like to know if it's possible for example I have one sql table with the following results
+----+---------+--------+--------+
| ID | Name | Number | Active |
+----+---------+--------+--------+
| 1 | Jessica | 12 | 0 |
| 2 | Andrew | 23 | 1 |
| 3 | Jason | 53 | 0 |
+----+---------+--------+--------+
And I would like to change the active field to 0 = No | 1 = Yes but only in the results, I don't want to change the value of the row, is it possible to make one query that can do it?
Well with the answers bellow I managed to get it changed but now how can I echo the value in php?
SELECT *, case when Active =0 then 'No' when Active =1 then 'Yes' end as Expr1,
FROM table
Should it be like: $isActive = $rows['Expr1'];
NVM the line above is working.

Just use a case statement for translating 1 = yes and 0 = No like this
select ID
,Name
,Number
,case when Active=0 then 'No'
when Active=1 then 'Yes'
end as active_y_n
from table

use case when
select Id,name,number,
case Active when 0 then 'No'
when 1 then 'Yes' end as active_status
from t

A particularly simple way would use elt():
select Id, name, number,
elt(Active + 1, 'No', 'Yes') as as active_status
from t

Related

SQL group by different values in different fields

I'm trying to make a query in my mySQL DB
I Have
Field 1 | Field 2 | url
v1 | Value01 | http:1
v1 | Value02 | http:2
v2 | Value02 | http:3
v3 | Value01 | http:4
v3 | Value02 | http:5
v2 | Value02 | http:6
I try to count the values from field 2 when they are repeated
all grouped by Field 1
I expect this
Field 1 | count Value01(from Field 2)|count Value02(from Field 2)
v1 | 1 | 1
v2 | 0 | 2
v3 | 1 | 1
is there a way to do something like that?
You use a conditional COUNT or SUM
You can remove the ELSE NULL because is the default return for CASE but I include it for clear reading.
.
SELECT
Field1,
COUNT( CASE
WHEN Field2 = 'Value01' THEN 1
ELSE NULL
END) as count_Value01,
SUM ( CASE
WHEN Field2 = 'Value02' THEN 1
ELSE 0
END) as count_Value02
FROM yourTable
GROUP BY Field1
You can do this using conditional aggregation. This is particularly simple in MySQL:
select field1, sum(field2 = 'Value1') as NumValue01,
sum(field2 = 'Value02') as NumValue2
from table t
group by field1;

How to get single row when foreign key is similar

I have a table tbl_issue with columns
> serial_no.
Issue_no. (f.k)
From_Section
To_Section
+-----+---------------+-------------+-------------+
| id | issue no | from section| to_section |
+-----+---------------+-------------+-------------+
| 1 | 223 | MFA | N/A |
| 2 | 223 | N/A | LOG |
+----------+----------+-------------+--------------+
When I query the table on issue no. I get two rows, can anyone kindly help how can I get a single record and no 'N/A'
For the example you gave this would work:
WITH combined AS
(
SELECT i.issue_no,
CASE WHEN i.from_section = 'N/A' THEN i2.from_section ELSE i.from_section END from_section,
CASE WHEN i.to_section = 'N/A' THEN i2.to_section ELSE i.to_section END to_section
FROM dbo.tbl_issue i
INNER JOIN dbo.tbl_issue i2
ON i2.issue_no = i.issue_no
)
SELECT DISTINCT *
FROM combined c
WHERE c.from_section <> 'N/A' AND c.to_section <> 'N/A'
This is supposing that the 'N/A' is does not mean NULL...If you meant NULL replace "= 'N/A'" with IS NULL and replace "<> 'N/A'" with IS NOT NULL

Mysql subquery for group and count records by specific field

I have this problem:
I have a car table, each car record has a state field, state field value can be:
1 = Enable or 2 = Disable
For example, in this case I need to show all cars grouped by color field
and counted by color, this is not problem really :)
Here the SQL statement :
SELECT
`id`,
`model_family`,
`color`,
COUNT(`color`) AS 'quantity',
`state`
FROM `auto`
WHERE `model_family` = 'Sedan'
GROUP BY `color`
+-----+--------------+------------+----------+---+
| id | model_family | color | quantity | state |
+-----+--------------+--------+----------+-------+
| 77 | Sedan | Red | 2 | 2 |
| 42 | Sedan | Blue | 3 | 2 |
| 97 | Sedan | Green | 5 | 1 |
+-----+--------------+--------+----------+-------+
Results show two Disabled records and one Enabled record.
Well, the questions is :
How can I can show the cars disabled and enabled but
For example, if for first grouped record if state is disabled (state = 2)
then quantity field will appear as "0" (because it not exists cars enabled)
else quantity = n
Something like this:
+-----+--------------+------------+----------+-------+
| id | model_family | color | quantity | state |
+-----+--------------+------------+----------+-------+
| 77 | Sedan | Red | 0 | 2 |
| 42 | Sedan | Blue | 0 | 2 |
| 97 | Sedan | Green | 5 | 1 |
+-----+--------------+------------+----------+-------+
Regads !
SELECT
`id`,
`model_family`,
`color`,
SUM(CASE WHEN state = 1 THEN 1 ELSE 0 END) as enabled,
SUM(CASE WHEN state = 2 THEN 1 ELSE 0 END) as disabled,
`state`
FROM `auto`
WHERE `model_family` = 'Sedan'
GROUP BY `color`
You have to use case statements to group the items together. When it finds that the state is 1 you will need to sum together all of those records which is why I am doing the Then 1 Else 0.
This was a quick example of how to do it. I haven't tested to make sure it works, but it should.
If you want all disabled groups to be replaced with 0, you can just put a single case statement in your select clause. Something like this:
SELECT id, model_family, color, (CASE WHEN state = 1 THEN COUNT(*) ELSE 0 END) AS quantity, state
FROM auto
WHERE model_family = 'Sedan'
GROUP BY color
ORDER BY id;
It tested fine with your data on SQL Fiddle.
EDIT Note, since you're grouping by color, you can just use COUNT(*) instead of COUNT(color) because they are going to count the same thing.
EDIT 2 Also important to note that if a color has some enabled vehicles and some disabled vehicles, it still returns 0 because there is at least one disabled. If you want a count of the enabled ones, you can do something like this:
SELECT id, model_family, color, SUM(CASE WHEN state = 1 THEN 1 ELSE 0 END) AS quantityEnabled, state
FROM auto
WHERE model_family = 'Sedan'
GROUP BY color
ORDER BY id;
This fiddle has both examples.

Return result via column view instead row view in mysql

I want to know if there is a way to return my result via column. Simple Select statement will return like
_________________
| id | name |
| 1 | abc |
| 2 | abc1 |
| 3 | abc2 |
is there a way to retrieve my result like this
| 1 | abc | 2 | abc1| 3 | abc2|
or like
|1 | 2 | 3 |
|abc |abc1|abc2|
i am only returning 4 columns, and i am using the data as header of other queries. Please help me. Thanks
Try This:
select group_concat(id,"|",name SEPARATOR '|') from T;
You can achieve this using PIVOT. You need to use MAX and GROUP BY to simulate PIVOT.
SELECT Id,
MAX(CASE WHEN Id = 1 THEN name END) col1,
MAX(CASE WHEN Id = 2 THEN name END) col2
FROM Table1
GROUP BY Id
You need to decide based on which criteria you need to group the data. Try this example.
Try this..
Query
SELECT
MAX
(
CASE id WHEN 1
THEN name
ELSE NULL END
) AS `1`,
MAX
(
CASE id WHEN 2
THEN name
ELSE NULL END
) AS `2`,
MAX
(
CASE id WHEN 3
THEN name
ELSE NULL END
) AS `3`
FROM tbl;
Demo
I solve my problem because they give me clue, now this is my code, I want to share it
SELECT
MIN( CONCAT(title,"\n", DATE_FORMAT(start_time,'%h:%i %p')) ) col1,
MAX( CONCAT(title,"\n", DATE_FORMAT(start_time,'%h:%i %p')) ) col2
from table
thanks everyone

MySQL Query that will group records

I have this table [Table 1]
cid | arrived | date_arrived
The [arrived field can have a value of [T] or [F], the value is [F] the date arrived field is NULL
1 records may appear only up to maximum of 2 (1 record for arrived=T and another record for arrived=F) But there are also records that may appear only once
1 | T | 2012-02-01
2 | F | [Null]
1 | F | [Null]
3 | T | 2012-03-05
I need a query that will show something like this
cid | arrived | not_arrived
1 Yes Yes
2 No Yes
3 Yes No
This works:
SELECT
cid,
SUM(arrived = 'T') > 0 as arrived,
SUM(arrived = 'F') > 0 as not_arrived
FROM [Table 1]
GROUP BY cid;
You can try it here: http://sqlfiddle.com/#!2/2b5a7/1/0
try
select cid,
case when find_in_set('T', group_concat(arrived)) > 0 then 'yes' else 'no' end as arrived,
case when find_in_set('F', group_concat(arrived)) > 0 then 'yes' else 'no' end as not_arrived
from table1
group by cid