ID | Name
---|-----
1 | John
2 | John
3 | Mike
4 | James
5 | Doe
I have this table. I want to query so that I'd be able to get this:
12 John
3 Mike
4 James
5 Doe
i've tried it with putting the variables with array but the result is only 12345 Doe. Will anybody please give me an idea?
Try this
SELECT GROUP_CONCAT(ID), NAME
FROM DB.TABLE
GROUP BY NAME
Related
Users:
UserID | UserName
--------------
1 Jack
2 Evelin
Shop:
ShopID | ShopName | ShopUserID
-------------------------------
1 Prague 1
2 Berlin 2
Is possible get automatically update table:
ShopID | ShopName | ShopUserID
-------------------------------
1 Prague Jack
2 Berlin Evelin
Can I use trigger?
Thanks!
Please use Join please try this query :
Select s.shopID,s.ShopName,u.UserName from Shop as s Left Join Users as u on s.ShopUserID = u.UserID
Output will look like this :
ShopID | ShopName | UserName
-------------------------------
1 Prague Jack
2 Berlin Evelin
I have a table filled with first and last names. I have two other columns that I am trying to update. These two columns has the number of people that have the same first names and same last names. For example,
first last samef samel
John Smith 1 2
John Adams 1 1
Mary Kate 0 0
Kate Adams 2 1
Kate Smith 2 2
Kate Smith 2 2
Alice Mirth 0 0
So far I can only come up with these two queries, but of course they are not correct. They return the total count for each name when I need the total count - 1. Plus, the results are shown on separate tables.
I was wondering if I should use a stored procedure where I use variables to store the count for samef and samel. And then insert it into the names table, but I don't know the correct syntax for this.
SELECT first, last,
( SELECT COUNT(*) FROM names WHERE first = table1.first) AS samef
FROM names AS table1
SELECT first, last,
( SELECT COUNT(*) FROM names WHERE last = table2.last) AS samel
FROM names AS table2
I am new to mySQL so please provide explanations.
Just like Strawberry mentioned, do not store information that can be derived. Databases are great at storing data optimally. SQL is great at extracting table and derived/calculated data. Try this:
select `first`, `last`,
(select count(*)-1 from test where `first` = t.`first`) as samef,
(select count(*)-1 from test where `last` = t.`last`) as samef
from test t;
Example: http://sqlfiddle.com/#!9/9c673f/1
Result:
| first | last | samef | samef |
|-------|-------|-------|-------|
| john | smith | 1 | 2 |
| john | adams | 1 | 1 |
| mary | kate | 0 | 0 |
| kate | adams | 2 | 1 |
| kate | smith | 2 | 2 |
| kate | smith | 2 | 2 |
| alice | mirth | 0 | 0 |
So I have a table where each row may or may not be merged via a merge_id.
Its easy to jump in and grab all merged rows, where the merge_id column is not null. The thing is I want to output the merged rows together in one container on the client side.
I there any way for sql to return a collection for merged collections ?
Do I sort in ascending order of the merge ID and trust that its sequential ?
EDIT
________________________________________________
|id | firstname | lastName | merge_id |
|------------------------------------------------|
| 1 | jane | Doe | 1 |
-------------------------------------------------|
|------------------------------------------------|
| 2 | John | Doe | 1 |
-------------------------------------------------|
|------------------------------------------------|
| 3 | max | payne | 2 |
-------------------------------------------------|
|------------------------------------------------|
| 4 | sub | zero | 3 |
--------------------------------------------------
So I want to query in such a way that I know that jane and max belong to different mergers.
How about
SELECT firstname, lastname, merge_id
FROM table t
ORDER BY t.merge_id
That would give you a record per person, and the merge_id will be ascending:
1 | Jane Doe
1 | John Doe
2 | max payne
3 | sub zero
Otherwise, you can use GROUP_CONCAT:
SELECT merge_id , GROUP_CONCAT(CONCAT(firstname, ' ', lastname))
FROM table t
GROUP BY t.merge_id
ORDER BY t.merge_id
Which will give one record per merge_id:
1 | Jane Doe, John Doe
2 | max payne
3 | sub zero
I have some records like this
id name sequence
------------------------
1 steve 3
2 lee 2
3 lisa 1
4 john 0
5 smith 0
I want to display records like following
id name
------------
1 lisa
2 lee
3 steve
4 john
5 smith
When i am using order by clause then it display like
name
----
john
smith
lisa
lee
steve
Query
SELECT name from tbl1 where 1 ORDER BY sequence ASC
SELECT name
FROM tbl1
ORDER BY sequence = 0,
sequence ASC
or
SELECT name
FROM tbl1
ORDER BY case when sequence <> 0 then 1 else 2 end,
sequence ASC
You can use query with if condition in ORDER BY clause
SELECT
name
from tbl1
ORDER BY IF(sequence = 0,name,sequence) ASC
Fiddle
Output
| NAME |
|-------|
| lisa |
| lee |
| steve |
| john |
| smith |
What I'm hoping to do is create a string out of a table WITHIN a query so that I may be able to place that string in another query I'm creating. Say, I have this for a table:
index | position | name
----------------------------------------
1 | member | John Smith
2 | chair | Mary Jones
3 | member | Mary Jones
4 | contact | Grace Adams
5 | director | Grace Adams
6 | member | Grace Adams
7 | treasurer | Bill McDonnell
8 | vice chair | Bill McDonnell
9 | member | Ishmael Rodriguez
I'm looking for the result as follows:
name | positions
----------------------------------------
John Smith | member
Mary Jones | chair,member
Grace Adams | contact,director,member
Bill McDonnell | treasurer,vice chair
Ishmael Rodriguez | member
I was hoping I could use some variant of CONCAT_WS() to get my result, like this...
SELECT
a.NAME,
CONCAT_WS(
',',
(
SELECT
position
FROM
TABLE
WHERE
NAME = a.NAME
)
)AS positions FROM ---------------
Obviously, this isn't working out for me. Any ideas?
Use GROUP_CONCAT[docs]
SELECT name, GROUP_CONCAT(position) result
FROM tableName
GROUP BY name
ORDER BY `index`
SQLFiddle Demo
Use GROUP_CONCAT like so:
SELECT name, GROUP_CONCAT(position SEPARATOR ',')
FROM Table
GROUP BY name