My problem is that I have a query that with group_concat gets me the result I need. this is the query. And I have already joined 3 tables where table1 has for example people names, table2 has their liked fruit info and table3 has their fruit names. In table2 they are numbered cause of for the need to alter the data for needs
SELECT
table1.row2,
table2.group_concat(distinct row2)
FROM table1
JOIN table2
WHERE
table1.row1=table2.row1
GROUP BY
table1.row1
The issue is that row 2 gives out numbers (example 1,2) but they are defined in another table into 1=apple and 2=orange
So i use a function with defines that
function fruit($value){
if ($value==1){
$result=apple;
}elseif($value==2){
$result=orange;
}
return $result;
}
So as i want to have a column where instead of "1,2" is "apple, orange" I use the function in the fetching results. Issue is that i dont get "apple,orange", instead I get "apple"
Seems like it just zeros my grouping
Looked for answers but couldnt find any. anybody ???
You can join onto this other table to get the names in the query:
SELECT row1,group_concat(distinct table2.name) FROM table
LEFT JOIN table2 ON (table.row2 = table2.id)
GROUP by row1
Obviously you'll need to change table2.id and table2.name to something appropriate.
As for the function you aren't returning $result so any calls to this will return NULL. Simply add:
return $result;
Related
I was able to join 3 tables and count the data set, bet getting the columns on specific table it only see the 3rd table column..
SELECT *
FROM `InvoiceItemTable` a
JOIN `InvoiceTable` b
ON (b.id = a.invoice)
JOIN `products` c
ON (a.product = c.id)
WHERE b.status='1' AND c.store = '2'
//$invoices = $inginger->fetch(PDO::FETCH_ASSOC))
echo $invoices['a.price'];
This price return error : Undefined index: a.price in...
there is "price" in the first table(InvoiceItemTable).
echo $invoices['invoice'];
There is invoice in first table(InvoiceItemTable) and it returns it works
I dont want to use $invoices['price'] because there is 'price' colum in the third table too and that is what it returns, I want to get the price in the first table. $invoices['price.InvoiceItemTable'] returns undefined index
php wont recognize $invoices['a.price']; you have to use $invoices['price'];
if you have same fieldname in multiple tables you have to create an alias
SELECT a.price as a_price, b.price as b_price, c.price as c_price
and then you can use $invoices['a_price']
In general, it's much better to explicitly define your columns. In doing so you can add a specific name to the the column you're interested in (or conversely change the name of the one you're not to remove ambiguity)
For examples sake, I'm just showing the columns you've described but it would look something like.
SELECT a.price as price, c.price as product_price
FROM `InvoiceItemTable` a
JOIN `InvoiceTable` b ON (b.id = a.invoice)
JOIN `products` c ON (a.product = c.id)
WHERE b.status='1' AND c.store = '2';
Doing naming such as this is more work, but insulates your code from changes and more explicitly shows the reader what is being returned.
I have a database with 2 tables. one called "object" and the other called "object_meta".
now i want to save additional fields depending to an entry in the "object" table inside the "object_meta" table. until now i do so with 2 queries.
1st query:
SELECT * FROM object WHERE id=$id
and the 2nd query:
SELECT * FROM object_meta WHERE object_id=$id
and then i put this 2nd query inside a new field of the 1st array called ex: meta_fields
including the array of the query to "object_meta"
$array[$id]['additional_fields'][$add_field[key]] = $add_field[value];
please can someone show me how to do this with a join?
thx a lot :D
SELECT object.*, object_meta.*
FROM object
INNER JOIN object_meta
ON object.id = object_meta.object_id
WHERE object.id = ?
Adjust the INNER JOIN to a LEFT JOIN if object_meta does not carry information for every object. The fields of object_meta will then be NULL if there is no matching row.
Note that this will not put the values into addtional_fields, but on the same level as all the other values.
This is how you can do it, however I would suggest to select only the required fields from both tables which you want to display and you can do as table_name.col_name1,table_name.col_name2 etc and can use where condition on first table col name with the input value.
select * from `object`
inner join `object_meta` on `object_meta`.`object_id` = `object`.`id`
I have two tables, Table A and Table B. For each record in table A there are many records in Table B; thus, a one to many relationship exists between tables A and B. I want to perform a query so that for each row returned from table A, all of the corresponding rows will be returned from table B. From what I understand I'll need to use a INNER Join - however, how would I go about accessing all of the returned rows through say, PHP?
$sql = "Select A.ID, B.Name * From A INNER JOIN B ON A.ID = B.ID";
A.ID | B.fName | B.lName
1 nameone lnameone
1 nametwo lnametwo
2 namethree lnamethree
4 namefour lnamefour
Now that I have the above results, I want to use PHP to loop through all of the values of B.Name only for a single A.ID at a time. So, the results I want would look like:
1.
nameOne lNameOne
nameTwo lnametwo
2. namethree lNamethree
4. nameFour lNameFour
Basically, I'm trying to group the query results by the ID in table A.
I appreciate the help very much!
Thank you,
Evan
You could just Google "php get from database," and use some normal array pre-processing, but I worry the advice you find may not be ideal. Here's what I'd do:
$pdo = new PDO('mysql:host=host;dbname=dbname', 'user', 'pass');
$result = $pdo->query(<<<SQL
SELECT
ID,
Name
FROM
A
-- Alternative to `JOIN B USING(ID)` or `JOIN B ON (A.ID = B.ID)
NATURAL JOIN B
SQL
);
$a = array();
while ($row = $result->fetch()) {
if (!isset($a[$row['ID']]) {
$a[$row['ID']] = array();
}
$a[$row['ID']][] = $row['Name'];
}
You could also GROUP BY the ID and GROUP_CONCAT the names to be exploded in PHP later to skip the manual array creation and reduce some iteration (although SQL will do more in that case).
Adding a simple ORDER BY A.ID to the SQL query would probably get you quite far in "grouping" the items together. It's difficult to give a more detailed answer without knowing exactly what you want to do with the "groups".
Sorry, guys.I am quite new in mysql but I do need help from getting and merging data from 2 tables.
table_a
ID | TITLE | CONTENT | DATE
table_b
ID | POST_ID | IMAGE
Here's my code
$query = "SELECT table_a.*, table_b.IMAGE FROM table_a
LEFT JOIN table_b
ON table_a.ID = table_b.POST_ID
ORDER BY table_a.DATE";
$mysql_result = mysql_query($query);
$result = array();
while ($row = mysql_fetch_assoc($mysql_result)) {
$result[] = $row;
}
print json_encode($result);
However, for those record in table_a which got more than 1 IMAGE, my json contain duplicated CONTENT with different IMAGES.
Is there any methods to merge IMAGE with the same ID into a single record?
Thanks for any helps!
You can use the GROUP_CONCAT function to group the images as a comma-delimited list in one column of your posts table.
If I understand correctly, you want to have all fields from table_a, and only one (maybe combined) field from table_b.
First of all, you have to decide what you want to get, if you have more than one image:
Only 1 image? Use MIN(table_b.IMAGE) or MAX(table_b.IMAGE) in the
following SQL
All images separated by e.g. a comma? Use GROUP_CONCAT(table_b.IMAGE SEPARATOR ',') or similar in the following SQL
Next you have to understand, that to get only one row per table_a.ID, you have to group by table_a.ID, so we have
SELECT
table_a.*,
<function from above> AS image
FROM table_a
LEFT JOIN table_b
ON table_a.ID = table_b.POST_ID
GROUP BY table_a.ID
ORDER BY table_a.DATE
I believe what you need is something like this:
SELECT table_a.*, GROUP_CONCAT(table_b.IMAGE) FROM table_a
LEFT JOIN table_b ON table_a.ID = table_b.POST_ID
GROUP BY table_a.*
ORDER BY table_a.DATE
(Not sure if you have to spell out the GROUP BY clause, listing field names individually, or whether .* notation will be accepted here.)
You can perhaps use JOIN instead of LEFT JOIN . In this way it will only load one row
or the other way
put GROUP BY a.ID jsut before ORDER BY...
What i want, to display rows from a table which is selected by a field from other table single value, lets say to display images from a table by last category id.
I have this type of query, but this return me all matching keys rows, if i inset LIMIT 1 then it return one row...
SELECT i.prof_image FROM profile_images i
JOIN images_cat cat ON (cat.cat_id = i.cat_id)
GROUP BY i.prof_image;
//OR LIMIT 1;
Any idea to fix this problem. (i.e. displaying the latest category images)?
This will work for your specific example.. If you need to be more selective, then please post some more details..
SELECT i.prof_image
FROM profile_images i
WHERE cat_id = (select max(cat_id) from images_cat)
SELECT * FROM table_1
LEFT JOIN table_2 ON table_1.id = table_2.id
This query will grab all things in table_2 that have the same id value.
Note that it is a LEFT JOIN - which means that if there are no matching values in table_2, it will still return the values from table_1.
What is your intent with using last()?
Hope this helps.