Use content of fieldnames in query - php

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.

Related

Fetch results in group based on the occurrence

Ok, I have a single MySQL table with the name 'car' and 3 columns.
+----+--------+------------+
| ID | car_id | engine |
+----+--------+------------+
| 1 | 123 | on |
| 2 | 123 | on |
| 3 | 123 | off |
| 4 | 123 | on |
| 5 | 123 | on |
| 6 | 123 | on |
| 7 | 123 | off |
| 8 | 123 | on |
| 9 | 123 | off |
+----+--------+------------+
Now I want to show the trips this car did. The trips would be determined based on car engine start and stop. For example from the above example we can see that user has made 3 trips as total(From on to off). Now What I want is that if there is a query which gives me only 3 results from on to off meaning if somehow the query groups the records by considering a starting point on and ending point off. Is it possible in mysql? or the other way around is doing it manually by fetching all the records and working in arrays?
At the moment I am fetching all the records and doing it manually by looping all the data and doing accordingly But this process is slow.
Can you try it ?
SELECT * from cars WHERE `engine` = 'off' AND id IN(SELECT id+1 FROM `cars` WHERE `engine` = 'on')

Get sum of a database field with where condition from multiple tables in PHP Mysql

I am having problem with fetching "sum" of a field from a table with multiple where conditions from multiple table in php MySQL.
For Example:
Table1:"Participant"
part_id | name | total_part | work_id
1 | Ravi | 2 | 102
2 | Sam | 1 | 102
3 | Mike | 3 | 101
Table2: "Workshop"
work_id | Month | Year
101 | March | 2
102 | April | 1
103 | May | 3
Form the above two table I want to select the sum of "total_part" from Participant with where condition of "work_id" and "Month" from workshop in single php mysql query.
Thanks in advance
Without having any context, it sounds like you're just looking for SELECT SUM(column) FROM table WHERE [clause 1] AND [clause 2]. Hope that helps.

MySQL Inserting a record after a specified location in a table

Consider a mysql table,
id | name | content | flag
------------------------------
1 | name1 | content1 | 0
2 | name2 | content2 | 1
3 | name3 | content3 | 0
4 | name4 | content4 | 0
I want to insert a new record before name3
5 | new | newc | 1
The Result should be like this,
id | name | content | flag
------------------------------
1 | name1 | content1 | 0
2 | name2 | content2 | 1
5 | new | newc | 1 <---- new record
3 | name3 | content3 | 0
4 | name4 | content4 | 0
Any MySQL query to do that?
P.S: I'm creating forum... so consider name3 posts something. The reply for name3 will be stored before name3 record as new. On displaying all the contents in the table in the reverse order, i could display the corresponding post and reply using mysql_fetch_array()
Relational databases don't have the concept of row order. If you care about the order that results are printed in a query, you must use the ORDER BY clause to specify the ordering based on the query data.
You'll have to remove the records which are after the record that you want to insert. There's no way to enter the data in specified order. However, for querying the table, you can use orderby.
You could potentially update every id after your location, making the new entry have an id of 3 in this case.

Mysql query, use field as column

I have the following table structure
+-------+------------+-----------+---------------+
| id |assigned_to | status | group_id |
+-------+------------+-----------+---------------+
| 1 | 1001 | 1 | 19 |
+-------+------------+-----------+---------------+
| 2 | 1001 | 2 | 19 |
+-------+------------+-----------+---------------+
| 3 | 1001 | 1 | 18 |
+-------+------------+-----------+---------------+
| 4 | 1002 | 2 | 19 |
+-------+------------+-----------+---------------+
| 5 | 1002 | 2 | 19 |
+-------+------------+-----------+---------------+
I would like to get the information in the following format
+-------+------------+-----------+
| | 1001 | 1002 |
+-------+------------+-----------+
| 1 | 1 | 0 |
+-------+------------+-----------+
| 2 | 1 | 2 |
+-------+------------+-----------+
So basically I am looking to use the assigned to field as the column names. Then the rows represent the status. So for example in the table we have two rows where user 1002 has a status of 2, therefore the sum is shown on that particular status row.
Please note that the group_id must be 19. Hence why I left out the row with id 3 on my table.
Can someone point me in the right direction. Im sure there is a name for this type of query, but I can't for the life of me put this into words. I have tried various other queries, but none of them even come close to this.
Marc B is right, there is no way to pivot a table -i.e. converting the content of a field into columns- unless you make some assumptions, like supossing that the values of assigned_to are somewhat fixed.
On the other hand, this is the kind of problems that can be solved by a program. It is not an easy program, but it can do the job.
I recently made a program similar to this in java, if you are interested I can post the core of it here.
you might want to read this article http://www.artfulsoftware.com/infotree/qrytip.php?id=523
i'd be something like
SELECT
assigned_to,
COUNT( CASE assigned_to WHEN '1001' THEN 1 ELSE 0 END ) AS '1001',
COUNT( CASE assigned_to WHEN '1002' THEN 1 ELSE 0 END ) AS '1002'
FROM table
WHERE group_by = 19
GROUP BY assigned_to WITH ROLLUP;
or something like that (i haven't tested this code.. )
in the article, he does it using SUM() you'd have to do it with COUNT() and add a WHERE constraint for the group_id
Hope this helps

I need assistance with a postgresql select query statement

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

Categories