I need assistance with a postgresql select query statement - php

I have been working with a postgresql database with a php project, and I am just trying to construct a query. This is the scenario.
Suppose I have several tables, FOO and BAR
Table FOO looks like this:
| foo_id | foo_name | foo_data |
----------------------------------
| 1 | john | son |
| 2 | jane | daughter |
| 3 | sam | son |
| 4 | sally | daughter |
Table BAR looks like this
| bar_id | bar_foo_id | bar_fooParent_id | bar_content |
----------------------------------
| 1 | 1 | 1 | yabba-dabba-doo |
| 2 | 2 | 1 | scooby-scooby-doo |
| 1 | 3 | 888 | don't have a cow, man |
| 2 | 4 | 999 | d'oh! |
I can't change the table schema, everything is as it is.
But - I can pass a value through PHP, let's call it PARENT, that represents a value from bar_fooParent_id (so I can pass 1,2,3...657,888,999 etc).
And - foo_id in table FOO maps to bar_foo_id in table BAR.
I would like to build a SELECT Query that combines data from both tables FOO and BAR. Something like:
SELECT BAR.bar_content, (and other BAR.columns) , FOO.foo_name, FOO.foo_data
FROM FOO,BAR WHERE bar_fooParent_id=".$PARENT." AND " ...?
where ? is a little confusing.
I need to grab rows for foo_name and foo_data from table FOO based upon the selected rows from table BAR. So if a value of 1 is passed to $PARENT, then bar_fooParent_id would be 1, I would get the first two rows from table BAR, and use their respective bar_foo_ids (with values 1 and 2) to grab the data from the rows of table FOO that have foo_ids of 1 and 2 (the first two rows in this case).
I have tried statements similar to those below (values are hard coded for simplicity)
SELECT * from BAR,FOO where BAR.bar_fooParent_id=1 AND (BAR.foo_id=1 OR BAR.foo_id=2) AND (FOO.foo_id=1 OR FOO.foo_id=3)
OR
select * from BAR where BAR.barr_fooParent_id=1 IN (SELECT foo_id,foo_name from kid WHERE foo_id=1 OR foo_id=3 )
without much success. Basically the data should return ideally as
foo_name | foo_data | bar_content | other BAR columns ... |
_________________________________________________________________
john | son | yabba-dabba-do | etc. |
jane | daughter | scooby-dooby-do| etc. |
(apologies for the formatting, not sure what is happening with this third table of results)
I'd appreciate it if someone can lend a hand in building this SELECT query for postgreSQL.
any ideas? and thanks.
Edward

Sounds like all you need is a simple join:
SELECT f.foo_name, f.foo_data, b.bar_content
FROM foo f
INNER JOIN bar b ON b.bar_foo_id = f.foo_id
WHERE b.bar_fooParent_id = 1

Related

sort results from database by cell value

i have database like this
============================
| id | name | value | key |
============================
| 1 | sara | | 1 |
============================
| 2 | sara | | 1 |
============================
| 3 | sara | 1 | |
============================
| 4 | jhon | | 1 |
============================
| 4 | jhon | 1 | |
============================
i want first to get only one result for each name
my expected output
jhon
sara
i use
select * from my_table
but it's display all names
and need to sort table by key cell
my expected output with sort
sara (3 keys)
jhon (1 key)
The function you are searching is called group by.
SELECT name, SUM(key) FROM my_table GROUP BY name
You can also use other aggregate function (not only SUM), maybe you want
SELECT name, COUNT(*) FROM my_table GROUP BY name
SELECT name, COUNT(key) FROM my_table GROUP BY name
Search for some examples for group by (also here on Stack Overflow) and check out the different results.
The other function you are searching for is called order by.
Please read some book or tutorials about sql, this is pretty basic stuff.

Using Two Tables in PHP & MySQL to display one loop result

Ok so a bit of a funny question here. I have two tables: -
LiveTable
ArchiveTable
The data is as follows: -
Table: LiveTable Table: ArchiveTable
| ID | NAME | | ID | NAME |
------------------ ------------------
| 1 | Test One | | 4 | Test Four |
------------------ ------------------
| 2 | Test Two | | 5 | Test Five |
------------------ ------------------
| 3 | Test Three| | 6 | Test Six |
What I want to do is merge them into one table for querying purposes only. Not as a Database Structure.
In essence when I do a PHP Loop I want the results to work like this: -
Merged Results
| ID | NAME |
------------------
| 1 | Test One |
------------------
| 2 | Test Two |
------------------
| 3 | Test Three|
------------------
| 4 | Test Four |
------------------
| 5 | Test Five |
------------------
| 6 | Test Six |
How would I go about doing this? Also is there a way of doing this with Doctrine?
You can use an SQL query with UNION:
SELECT ID, Name
FROM LiveTable
UNION ALL
SELECT ID, Name
FROM ArchiveTable
Note: UNION ALL will retain duplicates. If you want to remove duplicate records, then use UNION.
Yould use UNION:
SELECT id, name FROM tbl1
UNION ALL
SELECT id, name FROM tbl2
Create DB view using above two tables
CREATE VIEW view_name AS SELECT
id, name FROM tbl1
UNION
id, name FROM tbl2
Then you can query on your view
you can use the sql query multi table select
Select * from LiveTable,ArchiveTable
that's it

Mysql join query multiple values

I have a table called facility.
Structure looks as follows:
id | name
---------
1 | Hotel
2 | Hospital
3 | medical shop
I have an other table which is taking data from the above table and keeping multiple values in one column. View looks like below:
id | facilities
---------------
1 | Hospital~~medical shop~~Hotel
2 | Hospital~~Hotel
3 | medical shop~~Hotel
If I want to join these two tables how does the query look like?
I tried this, but it didn't work:
select overview.facilities as facility
from overview join facility on facility.id=overview.facilities;
you can do this with a bit of hackery
select o.facilities as facility
from overview o
join facility f on find_in_set(f.facilities, replace(o.facilities, '~~', ','));
I would highly recommend you change the way you are storing data. currently it is considered un normalized and that quickly becomes a monster to deal with
you should change your table structure to look something more like this
+----------+--------------+
| facility |
+----------+--------------+
| id | name |
+----------+--------------+
| 1 | Hotel |
| 2 | Hospital |
| 3 | medical shop |
+----------+--------------+
+-----------+-------------+
| overview |
+-----------+-------------+
| id | facility_id |
+-----------+-------------+
| 1 | 2 |
| 2 | 3 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 3 |
| 7 | 1 |
+-----------+-------------+
Code Explanation:
basically you are wanting to find the matching facilities in the overview. one handy function MySQL has is FIND_IN_SET() that allows you to find an item in a comma separated string aka find_in_set(25, '11,23,25,26) would return true and that matching row would be returned... you are separating your facilities with the delimiter ~~ which wont work with find_in_set... so I used REPLACE() to change the ~~ to a comma and then used that in the JOIN condition. you can go from here in multiple ways.. for instance lets say you want the facility id's for the overview.. you just add in the select GROUP_CONCAT(f.id) and you have all of the id's... note if you do that you need to add a GROUP BY at the end of your query to tell it how you want the results grouped

MySQL query how to get list of all distinct values from columns that contain multiple string values?

I am trying to get a list of distinct values from the columns out of a table.
Each column can contain multiple comma delimited values. I just want to eliminate duplicate values and come up with a list of unique values.
I know how to do this with PHP by grabbing the entire table and then looping the rows and placing the unique values into a unique array.
But can the same thing be done with a MySQL query?
My table looks something like this:
| ID | VALUES |
---------------------------------------------------
| 1 | Acadian,Dart,Monarch |
| 2 | Cadillac,Dart,Lincoln,Uplander |
| 3 | Acadian,Freestar,Saturn |
| 4 | Cadillac,Uplander |
| 5 | Dart |
| 6 | Dart,Cadillac,Freestar,Lincoln,Uplander |
So my list of unique VALUES would then contain:
Acadian
Cadillac
Dart
Freestar
Lincoln
Monarch
Saturn
Uplander
Can this be done with a MySQL call alone, or is there a need for some PHP sorting as well?
Thanks
Why would you store your data like this in a database? You deliberately nullify all the extensive querying features you would want to use a database for in the first place. Instead, have a table like this:
| valueID | groupID | name |
----------------------------------
| 1 | 1 | Acadian |
| 2 | 1 | Dart |
| 3 | 1 | Monarch |
| 4 | 2 | Cadillac |
| 2 | 2 | Dart |
Notice the different valueID for Dart compared to Matthew's suggestion. That's to have same values have the same valueID (you may want to refer to these later on, and you don't want to make the same mistake of not thinking ahead again, do you?). Then make the primary key contain both the valueID and the groupID.
Then, to answer your actual question, you can retrieve all distinct values through this query:
SELECT name FROM mytable GROUP BY valueID
(GROUP BY should perform better here than a DISTINCT since it shouldn't have to do a table scan)
I would suggest selecting (and splitting) into a temp table and then making a call against that.
First, there is apparently no split function in MySQL http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/ (this is three years old so someone can comment if this has changed?)
Push all of it into a temp table and select from there.
Better would be if it is possible to break these out into a table with this structure:
| ID | VALUES |AttachedRecordID |
---------------------------------------------------------------------
| 1 | Acadian | 1 |
| 2 | Dart | 1 |
| 3 | Monarch | 1 |
| 4 | Cadillac | 2 |
| 5 | Dart | 2 |
etc.

Use content of fieldnames in query

i have three mysql tables:
Table 456
id | binder | property1
1 | b | hello
2 | b | goodbye
3 | a | bonjour
Table binder
id | binder | tableid1 | tableid2
1 | a | 23 | 456
2 | b | 21 | 456
3 | c | 45 | 42
Table 21
id | property1 | data..
1 | goodbye | data about goodbye..
2 | ciao | data about ciao..
So first i want to select in binder the binder i need to get the tablesname where data is stored. So i need to select table by a fieldname in this case the fieldname is tableid1 and would have the content 21 so that i have to look in 21. AND it should be property 1 from table 456 and table 21 the same... i am using php and already tried with union and subquerys but it seems that i am to silly to prepare such query!
Normally if you need these things, you have a serious mistake in your database design. Instead of a table for each type, you need to have a column in your 'data' table the is the type. Of course that is only possible if they have the same type of data in them.

Categories