MySQL select all table data, gouped by one column data - mysql

I have a table that looks like this:
serial|vehicule|alert_emails
12411|AAA|yes
12411|BBB|yes
13411|CCC|yes
13411|DDD|yes
14411|EEE|yes
I want to do a mysql query to select all data and organize it by serial field to get a array result like this:
12411
AAA|yes
BBB|yes
13411
CCC|yes
DDD|yes
14411
EEE|yes
I tried group by the field serial but I'm not getting the desired result:
SELECT * FROM mytable GROUP BY serial;
Any help please?
Thanks.

Use UNION ALL to get the distinct serials of the table and all the rows of the table:
SELECT CASE WHEN t.col IS NULL THEN t.serial END serial, t.col
FROM (
SELECT DISTINCT serial, null AS col
FROM mytable
UNION ALL
SELECT serial, CONCAT(vehicule, '|', alert_emails)
FROM mytable
) t
ORDER BY t.serial, t.col IS NULL DESC
See the demo.

Related

My sql select that has multiple row in the same criteria

I have mysql table like this
I want to get row that has minimum 2 or more than 2 (multiple) row only from this table, so the result would be like this
What do i do?
thank you
Use GROUP BY and HAVING clauses
SELECT t.* FROM my_table t
JOIN (
SELECT cust_id, MIN(transaction_no) AS transaction_no
FROM my_table
GROUP BY cust_id
HAVING COUNT(cust_id) > 1
) agg ON t.transaction_no = agg.transaction_no

I want to merge two tables [Tabl1] and [Tabl2] and show result by ID where [Tabl1].[names] = 'any string name'?

In my example where name like '' show all value tabl2 with tabl1
SELECT *
FROM
(SELECT
ID, names, NULL AS address, work, note
FROM
Tabl1
UNION
SELECT
ID, name, address, NULL, NULL
FROM
Tabl2) as x
ORDER BY
id, note DESC, address
With CTE_NAME(ID, names) --Column names for Temporary table
AS
(
SELECT ID , NAME FROM TABLE1
UNION
SELECT ID , NAME FROM TABLE2
)
SELECT * FROM CTE_NAME --SELECT or USE CTE temporary Table
WHERE name = "x"
ORDER BY ID
You'll need to use UNION to combine the results of two queries. In your case:
SELECT ID, names, NULL AS address, work, note
FROM Tabl1
GROUP BY names
UNION ALL
SELECT ID, name, address, NULL, NULL
FROM Tabl2
GROUP BY Tabl3
Note - If you use UNION ALL as in above, it's no slower than running the two queries separately as it does no duplicate-checking.

how to select from a table which has two different values in a column?

========================================================
this is the sample db
I just want to get user who has both 2 and 14 in skills column. The answer should be "2"
Try this:
SELECT seekerID
FROM mytable
WHERE skillID IN (2, 14)
GROUP BY seekerID
HAVING COUNT(DISTINCT skillID) = 2
DISTINCT keyword is necessary only in case skillID values can occur multiple times for a single seekerID.
The easiest way to do this would be
select seekerID, count(*) as cnt
from table_name
where skillid in (2,14)
group by seekerID
having cnt = 2
use this:
select seekerID from table_name where skillid="2" and seekerID = ( select author from table_name where skillid="14")

How to write query to make a data divide into parts for same id

Here i like to explain my problem
Here i have a table like this
Company_name employee_id name dob father_name father_dob mother_name mother_dob
TCS EMP1201 Kalai 13/11/92 Mahendran 13/11/86 Amutha 15/7/88
CTS EMP1202 Naveen 13/11/92 raman 13/11/86 seetha 15/7/88
TCS EMP1203 Ganesh 13/11/92 Viijay 13/11/86 Sangetha 15/7/88
i need to write a query to show the father_name and father_dob seperately for employee_id belongs
same thing for mother_name and mother_dob too
i want to get a table like this,
employee_id name dob
EMP1201 Kalai 13/11/91
EMP1201 Mahendran 13/11/86
EMP1201 Amutha 15/7/88
so how can i write select query for this scenario.
Use UNION and finally order it by employee_id.
Query
select * from
(
select employee_id,name,dob
from table_name
union
select employee_id,father_name,father_dob
from table_name
union
select employee_id,mother_name,mother_dob
from table_name
)t
order by t.employee_id;
Fiddle demo
Update: If there is no(empty) father_name and father_dob,
select * from
(
select employee_id,name,dob
from table_name
union
select employee_id,father_name,father_dob
from table_name
where father_name is not null and father_dob is not null
union
select employee_id,mother_name,mother_dob
from table_name
where mother_name is not null and mother_dob is not null
)t
order by t.employee_id;
Check this fiddle.
Just use a union, something like:
SELECT employee_id, name, dob
FROM YourTable
UNION
SELECT employee_id, father_name, dob
FROM YourTable
Order the final dataset as desired.

Creating a query to display counts of specific results from a longtext field

Hello firstly I am a SQL newb and need a little help. I could not find a question exactly like mine.
(select count(*) from table where Column_x like '%text1%')
(select count(*) from table where Column_x like '%text2%')
(select count(*) from table where Column_x like '%text3%')
So I want the search to return
case 1 | Case 2 | Case 3
xtimes ytimes ztimes
Any help would be great, either just display a queried result or creating a temporary table.
Cheers,
The solution I found was as follows
Select * from(
(select count(*) as col1 from table where Column_x like '%text1%')col1,
(select count(*) as col2 from table where Column_x like '%text2%')col2,
(select count(*) as col3 from table where Column_x like '%text3%')col3
)
select * from (
(select count(*) from table where Column_x like '%text1%') as xtimes,
(select count(*) from table where Column_x like '%text2%') as ytimes,
(select count(*) from table where Column_x like '%text3%') as ztimes
);