Multiple SELECT statements from different tables - mysql

I have two independent tables: 'Clients' and 'Country'.
Country Table:
IdCountry Country
1 SPAIN
2 PORTUGAL
Clients Table
IdClient Entity IdCountry
1 Adam Alves 2
2 Peter Smith 2
3 David Ramos 1
4 Rafael Castro 1
I would like to add a new client into 'Clients' table but using the information from 'Country' table like this:
INSERT INTO Clients(IdClient, Entity, Country)
SELECT max(IdClient) + 1, '--New--' FROM Clients,
SELECT IdCountry FROM Country WHERE Country = 'SPAIN'
I would like to have this INPUT:
IdClient Entity IdCountry
5 --New-- 1
But if I run this query, it doesn't work. Could anybody help me, please?
COMMENTS: I prefer don't use autoincrement option.
Thank you very much.
Wardiam

You can do it like this:
INSERT INTO Clients(IdClient, Entity, Country)
SELECT
(SELECT MAX(IdClient) + 1 FROM Clients),
'--New--',
(SELECT IdCountry FROM Country WHERE Country = 'SPAIN')
See the demo.
Results:
| IdClient | Entity | Country |
| -------- | ------------- | ------- |
| 1 | Adam Alves | 2 |
| 2 | Peter Smith | 2 |
| 3 | David Ramos | 1 |
| 4 | Rafael Castro | 1 |
| 5 | --New-- | 1 |

Related

How to count the same values as one sql

I have a table like this.
----------------------------
| ID | CITY | BLOCK| NAME |
----------------------------
| 1 |Jakarta| A | John |
| 2 |Jakarta| A | Rey |
| 3 |Bekasi | A | Boy |
----------------------------
What's the correct query to count total of blocks where the city is Jakarta and if there are the same blocks in Jakarta, the blocks should be counted as one?
-----------------------
| TOTAL_BLOCK_JAKARTA |
-----------------------
| 1 |
-----------------------
If I'm using this query,
SELECT COUNT(block) FROM member AS total_block_jakarta WHERE city = 'Jakarta' GROUP BY block
it will return more than one row like this.
-----------------------
| TOTAL_BLOCK_JAKARTA |
-----------------------
| 1 |
| 1 |
-----------------------
If I'm using COUNT(DISTINCT) in above query it will return values like this.
-----------------------
| TOTAL_BLOCK_JAKARTA |
-----------------------
| 2 |
-----------------------
Please help me to find the correct query. Thanks.
try this
SELECT COUNT(DISTINCT block) AS total_block_jakarta FROM MEMBER WHERE CITY = 'Jakarta';
You don't need to group the block since you are referring on how many members are on the Jakarta with the same block.
EDIT:
SELECT COUNT(DISTINCT(block)) FROM member AS total_block_jakarta WHERE kota = 'Jakarta'
SELECT COUNT(DISTINCT BLOCK) AS total_block_jakarta FROM member WHERE CITY LIKE "Jakarta"
Output:
--------------------
total_block_jakarta
1
---------------------

Getting all results from 3 tables without repetition

I have 3 tables: sports, venues and instructors, they all have an id and a name. Instead of having 3 separate SELECT * FROM table I want to fetch the results with a single query so that the result is something like:
+----------+------------+----------+------------+---------------+-----------------+
| sport_id | sport_name | venue_id | venue_name | instructor_id | instructor_name |
+----------+------------+----------+------------+---------------+-----------------+
| 1 | Sport1 | 1 | Venue1 | 1 | Instructor1 |
| 2 | Sport2 | 2 | Venue2 | 2 | Instructor2 |
| 3 | Sport3 | | | 3 | Instructor3 |
| 4 | Sport4 | | | | |
+----------+------------+----------+------------+---------------+-----------------+
I tried
SELECT * FROM sports, venues, instructors
However there are at ton of repetitions.
EDIT: The 3 tables are not related to each other in any way.
Although I see limited use for getting all the values in one query, you could alway use UNION ALL to get all the values;
SELECT sport_id id, sport_name name, 'sport' type FROM sports
UNION ALL
SELECT venue_id id, venue_name name, 'venue' type FROM venues
UNION ALL
SELECT instructor_id id, instructor_name name, 'instructor' type FROM instructors
This will give output in the format;
id name type
-------------------------------
1 sport1 sport
2 sport2 sport
3 sport3 sport
4 sport4 sport
1 venue1 venue
2 venue2 venue
1 instructor1 instructor
2 instructor2 instructor
3 instructor3 instructor

Mysql data from table with condition

I have two mysql tables as follows:
contacts
---------------
id | name | email
---------------
1 | Jack | jack#test.com
2 | John | john#test.com
3 | Liz | liz#test.com
5 | Jack | jack#test.com
6 | Liz | liz#test.com
7 | Mike | mike#test.com
8 | Jack | jack#test.com
purchases
-------------------
id | contact_id | paid
-------------------
1 | 3 | true
2 | 5 | true
I need unique contact_ids that made purchase and other unique contact_ids that don't have made purchases.
So the final result will be as:
-------------------
id | name | email
------------------
2 | John | john#test.com
3 | Liz | liz#test.com
5 | Jack | jack#test.com
7 | Mike | mike#test.com
I tried the query as:
SELECT * FROM contacts LEFT JOIN purchases ON contacts.id = purchases.user_id
But this is not giving me unique rows as required. I tried several combination of DISTINCT, but I am not getting the result as required.
did you try this?
SELECT COALESCE(purchases.contact_id, contacts.id) as id, name, email
FROM contacts
LEFT JOIN purchases ON contacts.id = purchases.user_id
GROUP BY name
SQL FIDDLE
Something like that should work, but its performance is "?".
SELECT * FROM contacts WHERE id IN (
SELECT DISTINCT id as i FROM purchases
UNION
SELECT DISTINCT contact_id as i FROM purchases
)
GROUP BY id

Combining multiple rows into a comma delimited list in SQL

I have a MySQL table StudentName(id,name) that looks like this:
id | name
++++++++
1 | alex
1 | adam
1 | adnan
2 | ben
2 | bush
3 | cris
4 | daisi
4 | diana
And I'd like to make a new table like this:
id | name
+++++++++++
1 | alex, adam, adnan
2 | ben, bush
3 | cris
4 | daisi, diana
Is there a way to accomplish this?
The group_concat function is what you're looking for:
SELECT id, GROUP_CONCAT(name ORDER BY name ASC SEPARATOR ', ')
FROM my_table
GROUP BY id

Use MySQL to create a string out of a subquery?

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