Combining Result MYSQL - php

I wanted to combine few result into 1 table to be shown in PHP, bad thing is there is no unique key to identify it, Here is my mysql table data:-
ServiceGroup Name Moperator ShortCode
-------------- --------- ----------------- ---------
INTEGRAT norman KW_DTAC 11111
KW_I2C norman KW_ZAIN 00000
AB_MK norman HUT/TRUEMOVE 4541311
INTERGRAT amanda KW_DTAC 11111
In the table listed, the name amanda and norman has the same value, however I want the result to be shown like this on PHP.
ServiceGroup Name Moperator ShortCode
-------------- --------- ----------- ---------
INTEGRAT norman,amanda KW_DTAC 11111
KW_I2C norman KW_ZAIN 00000
AB_MK norman HUT/TRUEMOVE 4541311
How should I put it on PHP and what should the query looks like? This is example my query:-
SELECT Distinct userfilters.ServiceGroup, userdetails.Login, userfilters.Moperator, userfilters.ShortCode FROM userdetails INNER JOIN userfilters ON userfilters.UserDetailsID=userdetails.UserDetailsID AND Login IN ('amanda', 'brandon', 'bryan', 'christina', 'johnny', 'larry', 'monica', 'norman', 'eko') ORDER BY ServiceGroup DESC

Group the data by the unique columns and use grop_concat to put the login in a comma seperated list
SELECT f.ServiceGroup,
group_concat(d.Login) as logins,
f.Moperator,
f.ShortCode
FROM userdetails d
INNER JOIN userfilters f ON f.UserDetailsID = d.UserDetailsID
WHERE d.Login IN ('amanda', 'brandon', 'bryan', 'christina', 'johnny', 'larry', 'monica', 'norman', 'eko')
GROUP by f.ServiceGroup, f.Moperator, f.ShortCode
ORDER BY f.ServiceGroup DESC

Related

How to choose a value from another table using a JOIN

I just want to know one SQL query.
I've got two tables:
id | id_town
1 | 26
id | town
26 | Prague
What's the query if I need to print "Prague"?
Thank you.
PhpMyAdmin prints #1052 - Column 'id' in on clause is ambiguous
I tried this
SELECT town FROM localities JOIN towns ON id = id_town
TRY THIS:
select b.town
from table1 a
inner join table2 b on a.id_town = b.id

How to apply searching on two table using mysql

Hi i have two table one is parentlist and childlist.
I have have to apply searching on this table by parentname and childname. I have provided my table structure for better understanding, I have to apply searching on two different field of with different field name.
Fieldname is name in parentlist table and childname in childlist table.
I want below output if I type va then parentlist and childlist record should come in that query like below example. With this va Srting i have a
parentname varu123 and childname varu123 so I want these two record after executing the query.
This is the name of First table with fieldname
parentlist
............................................................
id name mobilenumber user_jid email
............................................................
1 varu123 123456 abc21 abc#gmail.com
2 abhishesk 123456 abc21 def#gmail.com
3 harsh 1234 def22 123#gmail.com
This is the name of Second table with fieldname
childlist
..........................................
id user_id childname Shoolname
...........................................
1 1 ram St.paul
2 1 raj St.xavier
3 2 varu123 St.paul
4 2 arun St.xavier
5 3 kapil St.paul
6 3 kamal St.xavier
I want this output: .
........................................................................................................
id name mobilenumber user_jid email childname Shoolname
..........................................................................................................
1 varu123 123456 abc21 abc#gmail.com ram,raj St.paul,St.xavier
2 abhishesk 123456 abc21 def#gmail.com varu123,arun St.paul,St.xavier
select pl.*, GROUP_CONCAT(cl.childname), GROUP_CONCAT(cl.Shoolname)
from parentlist as pl
inner join childlist as cl on pl.id=cl.user_id
where pl.name like '%va%' or cl.childname like '%va%'
use MySQL inner join or where :-
select * from parentlist inner join
childlist on parentlist.id=childlist.user_id
where childname='varu123' or parentlist.name='varu123'
use MySQL inner join or like :-
select * from parentlist inner join
childlist on parentlist.id=childlist.user_id
where childname like '%varu123%' or parentlist.name like '%varu123%'

Count and concatenate MySQL entries

Essentially, I have a table that is like this:
FirstName, LastName, Type
Mark, Jones, A
Jim, Smith, B
Joseph, Miller, A
Jim, Smith, A
Jim, Smith, C
Mark, Jones, C
What I need to do is be able to display these out in PHP/HTML, like:
Name | Total Count Per Name | All Type(s) Per Name
which would look like...
Mark Jones | 2 | A, C
Jim Smith | 3 | B, A, C
Joseph Miller | 1 | A
Jim Smith | 3 | B, A, C
Jim Smith | 3 | B, A, C
Mark Jones | 2 | A, C
I have spent time trying to create a new table based off the initial one, adding these fields, as well as looking at group_concat, array_count_values, COUNT, and DISTINCT, along with other loop/array options, and cannot figure this out.
I've found a number of answers that count and concatenate, but the problem here is I need to display each row with the total count/concatenation on each, instead of shortening it.
How about doing it like this?
SELECT aggregated.* FROM table_name t
LEFT JOIN (
SELECT
CONCAT(FirstName, ' ', LastName) AS Name,
COUNT(Type) AS `Total Count Per Name`,
GROUP_CONCAT(Type SEPARATOR ',') AS `All Type(s) Per Name`
FROM table_name
GROUP BY Name) AS aggregated
ON CONCAT(t.FirstName, ' ', t.LastName) = aggregated.Name
Without an ORDER BY clause, the order the rows will be returned in is indeterminate. Nothing wrong with that, by my personal preference is to have the result to be repeatable.
We can use an "inline view" (MySQL calls it a derived table) to get the count and the concatenation of the Type values for (FirstName,LastName).
And then perform a join operation to match the rows from the inline view to each row in the detail table.
SELECT CONCAT(d.FirstName,' ',d.LastName) AS name
, c.total_coount_per_name
, c.all_types_per_name
FROM mytable d
JOIN ( SELECT b.FirstName
, b.LastName
, GROUP_CONCAT(DISTINCT b.Type ORDER BY b.Type) AS all_types_per_name
, COUNT(*) AS total_count_per_name
FROM mytable b
GROUP
BY b.FirstName
, b.LastName
) c
ON c.FirstName = d.FirstName
AND c.Last_name = d.LastName
ORDER BY d.FirstName, d.LastName
If you have an id column or some other "sequence" column, you can use that to specify the order the rows are to be returned; same thing in the GROUP_CONCAT function. You can omit the DISTINCT keyword from the GROUP_CONCAT if you want repeated values... 'B,A,B,B,C',

Return all the rows even the joined table has empty results

In my table 1 I have something like this
name | age
George 42
Bob 30
Ken 23
In my table 2, I have something like this, this is where i store votes for each person.
name | votes |
George 1
Ken 1
George 1
George 1
Ken 1
My goal is to combine the 2 tables, and return all the rows in table 1 even it doesn't exist in table 2.
Desire results:
name | age | total_votes
George 42 3
Bob 30 0
Ken 23 2
But instead I get:
name | age | total_votes
George 42 3
Ken 23 2
I have tried something like this
SELECT `table_1`.*, coalesce(COUNT(`table_2`.votes), 0) AS total_votes
FROM `table_1`
LEFT JOIN `table_2`
ON `table_1`.name = `table_2`.name
You can do one of these:
1) Use Right Join instead of current Left Join.
Or
2) Exchange table1 and table2 places in your join expression, like:
FROM table_2
LEFT JOIN table_1
Try this. This works in MS Access , I think this will work on your's too just convert the query to SQL:
SELECT Table1.name, First(Table1.age) AS age, Count(Table2.Votes) AS totalVotes
FROM Table1 LEFT JOIN Table2 ON Table1.name = Table2.name
GROUP BY Table1.name;
Left Join table1 to table2 so that all entry from table1 , even if its is corresponding data is null, will be included. GROUP BY your query by name so that votes will be counted by name .

MySQL selecting row for attendance case

I have these tables:
table 1 : attendance
-------------------------------
ID | DATE | EMPLOYEE_ID |
-------------------------------
1 2013-09-10 1
2 2013-09-10 2
3 2013-09-10 3
-------------------------------
table 2: employee
---------------
ID | NAME |
---------------
1 Smith
2 John
3 Mark
4 Kyle
5 Susan
6 Jim
---------------
My actual code to show employee option.
while($row=mysql_fetch_array($query)){
echo "<option value='$row[employee_id]'>$row[first_name] $row[last_name]</option>";
}
?>
How can i show the list of employee that not registered in table 1?
The condition is if an employee already registered in table 1, they won't appear on the option.
I want to show the list in <option>'s of <select> element. So it will return: kyle, susan, jim.
Please tell me the correct query or if there is any better option, it'll be good too. Please give some solution and explain. Thank you very much
UPDATE / EDIT:
it also based on current date, if in table 1 have no latest date e.g. today it's 2013-09-15. It will show all of employee.
You can do this with a left join and then checking for no matches:
select e.*
from employee e left outer join
attendance a
on e.id = a.employee_id
where a.employee_id is null;
This is probably the most efficient option in MySQL.
EDIT:
To include a particular date, add the condition to the on clause:
select e.*
from employee e left outer join
attendance a
on e.id = a.employee_id and a.date = date('2013-09-20')
where a.employee_id is null;
If I understood correctly this should work, get all employees whose ID is not in the attendance table.
SELECT * FROM employee WHERE employee.ID NOT IN
(SELECT EMPLOYEE_ID FROM attendance)

Categories