pull combination of 2 tables from database - php

I need to pull combinations of 2 columns from two different tables from the same database.
ex:table1 has columns
Org_Id Org_Name
1001 company1
1002 company2
table2 has columns
Country_Id Country_Name
1 USA
2 uk
3 australia
4 canada
after creating combinations ,i need to create table 3 which hold the values of combinations...
table3 should have columns
org_name Country_Name
company1 usa
company2 uk
company2 usa
company1 canada
Note: Using joint we can display what ever we have in columns ,, but i need combinations of both the columns....
please help me this.....expecting your response asap....Thanks you all...

It's hard to tell from that how the 'combined' table should detect data, but normally that is done using views. Look into that.
Just noticed that in the manual there is actually an example that does pretty much what you described.

as described in example these two tables are not connected each other..
then you can directly take join without any joint condition ..
select Org_Name , Country_Name
from table1, table2
but as per practice this approach is not correct.. you should join tables with connected tables to show valuable result..

After you have figured how you want to join the tables, you could use INSERT ...SELECT
INSERT into table3(orgname,countryname) SELECT orgname,countryname from table1,table2;

You can create the new table and insert all the combinations in one fell swoop using SELECT INTO as follows:
SELECT org_Name, Country_Name
INTO table3
FROM table1, table2

Related

How to display values from 3 fields from different tables into one column?

I would like to combine a field from three different tables that has the same type of result.
For example:
Table: 1
Column: a.country_name
Possible Values: United States, Canada, Germany
Table: 2
Column: b.country_name
Possible Values: Armenia, Poland, Turkey
Table: 3
Column: c.country_name
Possible Values: Portugal, Japan, Argentina
I am not having a problem joining these tables together (with a general 'countries' table linking each of these three with a country_id field). What I am having an issue with is I don't know how to combine these into 1 column.
I have tried doing:
COALESCE(a.country_name, b.country_name, c.country_name)
But this does not include all the possible values. Lets say a.country_name would return a result set of 4 countries, but b and c return 2. Only 2 would show when I would like all 4.
I would just like one column, that returns all of the possible values (they are all countries)
Such as:
Country
United States
Canada
Germany
Armenia
Poland
Turkey
Portugal
Japan
Argentina
It sounds like what you want is UNION:
SELECT a.country_name as country_name
from a
UNION
SELECT b.country_name as country_name
from b
SELECT c.country_name as country_name
from c;
If I understand your question correctly, I think you want UNION / UNION ALL.
SELECT country_name FROM a
UNION ALL
SELECT country_name FROM b
UNION ALL
SELECT country_name FROM c
I think you are looking for GROUP BY 'country_name'. More information on W3.

sticky rows : fetching some rows always on top of list in postgresql

Im fetching records from a table, say countries, I always need few rows on top(in first of result set)
ID country
--------------------------
1 China
2 Japan
3 Taiwan
4 USA
5 Germany
6 Brazil
7 India
--------------------------
I want to keep Japan and China in the list top and then sort the rest
so the result set always should be
----------------------
Japan
China
next country
next country
next country
next country
----------------------
is there any way to do this using CTE or any other way that is right for this requirement?
Im using PostgreSQL 9.4
You can just pick those two countries out then add rest of them, something like:
select * from table
where country ='China' or country = 'Japan'
union select * from table;
union already remove the duplicated rows for you.
Hope this helps.
I use this order by technique in mysql but should work in postgresql just as well.
SELECT * FROM table ORDER BY FIELD(country, 'Japan', 'China') DESC;
One oddity or illogical bit at least in mysql is the query above will list China then Japan then all the rest.
You can achieve this objective using following query:
select your_column_list,
(CASE country WHEN 'Japan' THEN 0 WHEN 'China' THEN 1 ELSE 2 END) AS nu
from your_table_name
ORDER BY nu ASC
It depends where is kept the information of the sticky countries.
If this information is kept directly in the country table, since true > false you can just ORDER BY is_sticky DESC, country_name ASC
If this information is filled by the user and you know the country_id, then you can leverage the use of VALUES to keep the wanted countries on top list:
SELECT
label
FROM
country
LEFT JOIN (VALUES (3), (4)) ontop (label_id) USING (label_id)
ORDER BY
ontop.label_id IS NOT NULL DESC,
label ASC
Of course you could JOIN directly on the country name but that is slower.

Trying to display 2 tables data

I have 2 tables
Table name Dist.
NAME|Phone|ID
----------------
Oakley|555-555|1
Maui|666-666|2
lux|777-7777|3
Table name Patientacct.
Name|prescription|id|orderfrom
-------------------------------
bob|20-20|1|oakley
billy|15-20|2|Oakley, Maui
kim|20-20|3|Lux
I'm looking for a display like
Oakley
--------------------
Bob
Billy
Maui
------------------
Billy
Lux
--------------
kim
Trials so far
SELECT *
FROM `dist`
JOIN `patientacct` ON patientacct.dist LIKE CONCAT('%', `dist`.name ,'%')
GROUP BY `dist`.name
This showed only 1 dist
If I drop the group by example:
SELECT *
FROM `dist`
JOIN `patientacct` ON patientacct.dist LIKE CONCAT('%', `dist`.name ,'%')
I get the record twice so what I need is somewhere between the two. I'm extremely new to joins so be easy when explaining.
you must change your tables structure and you need to add a relation table with this values:
orders:
dist_id, patientacct_id PRIMARY_KEY(dist_id, patientacct_id)
First of all, please make the table structure relational. Have a primary auto_increment value in each table and use the primary key from the Dlist foreign key to Patientacct. This way, a simple join would fetch the record you have wanted.
Regarding the display, what do you mean you don't want record twice. Since you are using join, you will get two records for Oakley with the patient Bob and Billy. That's what you wanted and it would be like that by a simple join too in a relational table.

PHP and MySQL many-to-many question

Still wrapping my head around SQL and PHP, but hope someone can help with this:
I have the following tables:
1.
user table
- id
- name
- email
2.
user_group table
- user_id
- group_id
3.
group table
- id
- group_name
There is a many-to-many relationship between the user table and the group table. Now what I am trying to do build a browse users page which lists all the users in the system along with the groups that they belong to, so the page would look something like this:
Name: John Doe
Groups: football, tennis, swimming
Name: Jane Doe
Groups: hockey, basketball
Name: Jim Doe
Groups: hockey, football, rugby
etc. etc.
To accomplish this, I have the following SQL:
SELECT `user`.name, `group`.name
FROM `user`, `user_group`, `group`
WHERE `user`.id = `user_group`.user_id
AND `group`.id = `user_group`.group_id
GROUP BY `user`.id, `group`.id
which returns results as follows:
1. John Doe | football
2. John Doe | tennis
3. John Doe | swimming
4. Jane Doe | hockey
5. Jane Doe | basketball
etc. etc.
As you can see, the results returned need to be manipulated in order to produce the comma separated groups shown earlier, as .
Is there a simple way to get the page to display the groups so that they are in a comma separated list for each user in MySQL? Or do I have to write PHP code to loop through the results looking for duplicate IDs and generating the comma-separated lists of groups on the page? Or am I doing something completely wrong in my approach?
Many thanks.
There are a few options (in order of my personal preference).
Don't group by user id, and iterate trough your result and create an multi dimensional array using the user id as a key.
Use GROUP_CONCAT, which isn't pretty.
Use separate queries for selecting all groups + users, and iterate to create an multi dimensional array.
It makes no sense to have group_id in your user table if it is many-to-many and you already have a connecting table.
From PHP I guess you use MySQL, so you can use GROUP_CONCAT in situations like this.
Anyway, you are querying a hierarchical structure, which can not gracefully flatted to a single table, so you will always have to do some PHP coding to get the hierchical structure back.
try with GROUP_CONCAT with INNER JOIN
SELECT user.name, GROUP_CONCAT(group.name) FROM user
INNER JOIN user_group ON user.id = user_group.user_id
INNER JOIN group ON group.id = user_group.group_id
GROUP BY user.name
You're in luck. MySQL has a very handy aggregation operator group_concat which allows you to collapse the grouped results into a single row. In your case it would go something like this:
SELECT
`user`.name,
GROUP_CONCAT(`group`.group_name)
FROM `user`
INNER JOIN `user_group` ON (`user_group`.user_id = `user`.user_id)
INNER JOIN `group` ON (`group`.group_id = `user_group`.group_id)
GROUP BY `user`.name
This will match your table structure :)
SELECT `user`.name, GROUP_CONCAT( `group`.group_name )
FROM `user`
INNER JOIN `user_group` ON ( `user_group`.user_id = `user`.id )
INNER JOIN `group` ON ( `group`.id = `user_group`.group_id )
GROUP BY `user`.name
HTH :)

mysql multi table foreign keys? - beginner

I have 2 tables,
Table1:
id, int1, int2, int3
john,1,2,4
tim,2,3,4
pete,1,3,4
Table2:
integer,blob
1,wins
2,backtickle
3,text
4,whatever
The query I want to use is given the id I want to get the blob data from table2 associated with each of the integer columns in table1.
What is the specific query I can use here?
Sample result I am looking for would be something like:
search John returns "wins","backtickle","whatever"
search pete returns "wins","text","whatever"
I think it has something to do with foreign keys, but not sure...beginner level please! With 1 table it would be SELECT * FROM table1 WHERE id="........" but not sure with the setup i have now given above.
The structure of your database does not look optimal. You're limiting yourself to 3 items per person, and you're using columns instead of rows in order to list them. What you actually have in your data is a many-to-many relationship between Table1 and Table2. What I'd recommend is using three tables:
Persons:
name, personid
john,1
tim,2
pete,3
PersonBlobs:
personid, blobid
1,1
1,2
1,4
2,2
2,3
2,4
3,1
3,3
3,4
Blobs:
blobid,blob
1,wins
2,backtickle
3,text
4,whatever
PersonBlobs would give you the many-to-many link between Persons and Blobs.
And then the query becomes:
select Blobs.blob
from Blobs inner join PersonBlobs on Blobs.blobid = PersonBlobs.blobid
inner join Persons on PersonBlobs.personid = Persons.personid
where Persons.name = 'John'

Categories