Trying to display 2 tables data - php

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.

Related

Trying to write count query loop based on a different table printing

I have two tables:
access(name, id, check, key)
events(name, key, event_name)
I am trying to print some things from these tables into a php/html table with these columns:
name, key, event_name, access count
My trouble being I would like each event to display the "count" of access rows that have the same key.
Event table example:
name key event_name
test 1 first
joe 2 second
And the access table...
name id check key
test 123 yes 1
test 1235 yes 1
joe 175 yes 2
joe 852 yes 2
test 5843 yes 1
test 123455 yes 1
The resulting table I am hoping to look like this:
name key event_name access count
test 1 first 4
joe 2 second 2
Does anybody know how to do this? I've gotten to this but it obviously doesn't work because the key isn't given to the inner select query...
select event_name, name, key,
(SELECT COUNT(key) FROM access WHERE key=key AND name=name)
from event;
Thank you to anyone who takes a look and might have any ideas! I've been staring at this and w3schools for hours
At present your subquery will return a count of all rows as it is not correlated to the main query, so both occurrences of key in key=key will refer to the same column and the expression will always be true (likewise for name). To correlate the subquery, add table references:
select event_name, name, key,
(SELECT COUNT(key) FROM access a WHERE a.key=e.key AND a.name=e.name) AS `access count`
from event e
You can also get the same results with a join and aggregattion:
select e.name, e.key, e.event_name, count(*) access_count
from event e
left join access a on a.key = e.key and a.name = e.name
group by e.name, e.key, e.event_name

SQL - create relation to echo data from two tables

I made some researchs but couldnt find yet the exact concept needed to achieve this:
::: EDIT ::: my initial request wasnt clear so here I try again
I have two tables,
table_01 with four columns: id / name / address / id_cat
AND
table_02 with two columns: id_cat / category
id_cat in table_01 and table_02 is just an INT(10), while category is a VARCHAR supposed to contain the exact name of the category (which is quite long). What I am trying to do is, when a query is made on table_01, the echo shows rows of table_01 as result, but instead of showing id_cat as a number, it shows the text from table_02 category.
The result shows as I am expecting it, but, if for example in table_02, I have a rowid_cat = 1 , category = AAAAA, I would like to echo the id_cat from table_01 as the category from table_02, so still for example, 002 | standon | 125 market street | AAAAA instead of what I get now, which is 002 | standon | 125 market street | 1.
I didnt create relation between both tables yet.
If I understand what you are after, your query for the data would just need to be something like:
select t1.id as id, t2.category as category from tab_01 as t1
left join tab_02 as t2 on t1.id_cat = t2.id_cat where t1.id = '5';
The 5 is obviously the id you are looking for in tab_01.
I am not fully clear about your question. But I think this will help you.
Right now you have created one-to-many relationship between this two tables. This means one category (tab_02) can have multiple tab_01 data.
You can try this query:
SELECT t1.id_cat AS id_cat_number, t2.*
FROM tab_02 t2
INNER JOIN tab_01 t1 ON t1.id_cat = t2.id_cat
WHERE t2.category = XXXXXXXX

Grouping by Column with Dependence on another Column

Here is a simplified look at the problem I am trying to cleanly solve via a MySQL query. This is not the actual table I am dealing with.
If I have the following table:
Name Buyer ID
John Fred 4
John Smith 3
Fred Sally 2
John Kelly 1
I would like a query to return the following:
Name Buyer ID
John Fred 4
Fred Sally 2
Such that we group by 'name' and show the latest row / buyer / ID.
I tried to implement this by performing a nested select statement, wherein I first performed "ORDER BY ID DESC" then, on the outermost SELECT, "GROUP BY NAME". And, while this is a roundabout way of solving the problem, it seemed that, by virtue of the ordering, the correct selection would be returned to me. Unfortunately, "GROUP BY" does not 'guarantee' that the 'Buyer' column will contain the expected entry.
Any helpful suggests for implementing this as a query? At the moment, I have a highly-inefficient PHP 'version' of the query running on a large table dump - definitely not the best choice.
Try this one, the idea behind the subquery is that it gets the latest ID for each Name using MAX (aggregate function). Then join it against the table itself on the two columns of the subquery.
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT name, MAX(ID) maxID
FROM tableName
GROUP BY name
) b ON a.Name = b.Name AND
a.ID = b.MaxID
SQLFiddle Demo
Another alternative is to load the data sorted in a subquery, then group on the results. I can't cite this, but I've read in a few places there's no (discernable) performance hit on this.
So something like:
SELECT *
FROM (
SELECT *
FROM `yourtable`
ORDER BY `id` DESC
) as `tmp`
GROUP BY `name`

pull combination of 2 tables from database

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

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