MySql displaying results in same order no matter "array-order" - php

I am using "solr" search engine to query an index for classifieds that match a given criteria. The results are the ID:numbers of the classifieds, which I then use to find all matches in a MySql database with those ID:s.
The ID:s returned are put into an array.
As you can see below the array is imploded.
Then I use the "IN" to find all matches.
$solr_id_arr_imploded = implode("', '", $solr_id_arr);
$query = "SELECT mt.*, $sql_tbl.* FROM classified mt LEFT JOIN $sql_tbl ON
$sql_tbl.classified_id = mt.classified_id WHERE mt.ad_id IN ('$solr_id_arr_imploded')";
$sql_tbl is the category chosen by the user, in this case lets say it is "cars".
My problem is this:
I have the ID:numbers in an order (inside the array), but MySql doens't "care" about this order. MySql displays the oldest item first no matter what order the array is in.
So here is one same query displayed with two different "array-directions":
SELECT mt.*, fordon.* FROM classified mt LEFT JOIN fordon ON fordon.classified_id = mt.classified_id WHERE mt.ad_id IN ('Bmw_520i_Svensksald_784332731', 'Bmw_M3_Svensksald_755599519', 'Bmw_M3_E46_Full-utrustad_338210082')
SELECT mt.*, fordon.* FROM classified mt LEFT JOIN fordon ON fordon.classified_id = mt.classified_id WHERE mt.ad_id IN ('Bmw_M3_E46_Full-utrustad_338210082', 'Bmw_M3_Svensksald_755599519', 'Bmw_520i_Svensksald_784332731')
As you can see the ID:s are reversed in the second query above... But they are still displayed in the same order anyways. Why?
Should I use some other method of finding all MySql matches with ID:s from an array?
Ideas?
Thanks

This should do it:
SELECT mt.*, $sql_tbl.* FROM classified mt
LEFT JOIN $sql_tbl
ON $sql_tbl.classified_id = mt.classified_id
WHERE mt.ad_id IN ('$solr_id_arr_imploded')
ORDER BY FIELD(mt.ad_id,'$solr_id_arr_imploded')
See Order By Field in Sorting Rows.

MySQL will return the data in the order it "wants" (I suppose it'll be the order of the clustered index, or something like that), if you do not specify an order by clause.
If you want to change the order in which MySQL returns the results, you'll have to add an order by clause.
If that's not possible in your case, you'll have to re-order the elements from the PHP code -- for instance, instead of displaying the results from what MySQL returns, you should iterate over the list of ids returned by Solr, and display the results starting from there.
Basically, you'll first execute the MySQL query to fetch the results :
SELECT mt.*, fordon.*
FROM classified mt
LEFT JOIN fordon ON fordon.classified_id = mt.classified_id WHERE mt.ad_id IN (
'Bmw_520i_Svensksald_784332731', 'Bmw_M3_Svensksald_755599519',
'Bmw_M3_E46_Full-utrustad_338210082'
)
Then you can loop over those results, in PHP, storing them in an associative array (pseudo-code) :
$hash = array();
foreach ($db_results as $elem) {
$hash[$elem->ad_id] = $elem;
}
$hash will contain the data, indexed by id.
And, then, you'll display the data, using what Solr returned as a starting point for the loop (pseudo-code) :
foreach ($solr_results as $id_solr) {
echo $hash[$id_solr]->some_field . '<br />';
}
With this, you will :
display the results in the order returned by Solr
not do an additionnal (and possibily costly) sort on the database-side.

Related

How can I solve an problem with two select statements in one query

I want to retrieve og tags with sql in php language but I only get to see 1 result, that is the first one he reads, the other I don't get to see in page source.
this is the code with php.
$query = "SELECT metatitle FROM isacontent_content WHERE contentid = 12245
UNION ALL
SELECT name FROM isacontent_module_anchorimage WHERE contentid = 12245";
$resimage = $conn->query($query);
if(is_array($resimage)){
foreach ($resimage as $resItem){
$metaData[] = $resItem->fetch_assoc();
}
}else{
$metaData[] = $resimage->fetch_assoc();
}
$title = $metaData[0]["metatitle"];
$image = $metaData[0]["name"];
I expect that both select statements will work and I can see both contents in the meta tags
For UNION ALL, your column name must be same or you can use ALIAS for this.
but, here in your example, you can simply use INNER JOIN to get the both values from 2 tables by using 1 single query.
Example:
SELECT ic.metatitle, im.name FROM isacontent_content ic
INNER JOIN isacontent_module_anchorimage im ON im.contentid = ic.contentid
WHERE ic.contentid = 12245
Using INNER JOIN because your both tables having relation, so you can simply use INNER JOIN
Side Note:
If you know, your query will return 1 row then why are you storing data into an array here $metaData[]? you can simply store $title and $image inside you foreach() loop.
When you use union, your columns have to be in same number as it will combine results of two queries. In your case your asking for an particular content results which are stored in multiple tables, so you can go for joins.

MySQL automaticly delete duplicate row

I want to display all data from the function below, however whenever the function is called, MySQL only returns one row if there are duplicates. What is the problem with my SQL Query?
$sql_response1 = "SELECT *
FROM ".TABLE_PREFIX."tests_answers A, ".TABLE_PREFIX."tests_results R, ".TABLE_PREFIX."members M
WHERE A.question_id=".$q_id." AND R.result_id=A.result_id AND R.final_score<>'' AND R.test_id=".$_GET['tid']." AND M.member_id =A.member_id AND M.member_id IN ($in_text)
GROUP BY A.answer ORDER BY A.answer";
$result_responsed = mysql_query($sql_response1, $db);
while ($row_answer = mysql_fetch_array($result_responsed)) {
$x++;
$response_member = $row_answer['first_name'];
$response_answer = $row_answer['answer'];
$response_department = $row_answer['department_name'];
$x--;
$objPHPExcel->getActiveSheet()->setCellValue(A.$x, $response_answer.' '.'('.$response_member.' - '.$response_department.')');
$x++;
}
Let's say that the column result_id has two instances of the value foo, only one row will be returned. How do I show ALL results without MySQL truncating duplicate rows?
Your problem is that GROUP BY groups together - basically, collapses - all rows with the specified value in common. In this case, that means it will show only one row with a given value for A.answer.
You're using the GROUP BY query which will return only one result and it will remove duplicates. If you're trying to sort the results use ORDER BY without using GROUP BY as well.
$sql_response1 = "SELECT *
FROM ".TABLE_PREFIX."tests_answers A, ".TABLE_PREFIX."tests_results R, ".TABLE_PREFIX."members M
WHERE A.question_id=".$q_id." AND R.result_id=A.result_id AND R.final_score<>'' AND R.test_id=".$_GET['tid']." AND M.member_id =A.member_id AND M.member_id IN ($in_text)
ORDER BY A.answer";
For more background information on the GROUP BY function check out the informative page on W3Schools

Left join MySql/PHP

Whilst populating a table based on ids and labels from different tables, it appeared apparent there must potentially be a better way of achieving the same result with less code and a more direct approach using LEFT JOIN but i am puzzled after trying to work out if its actually capable of achieving the desired result.
Am i correct in thinking a LEFT JOIN is usable in this instance?
Referencing two tables against one another where one lists id's related to another table and that other table has the titles allocated for each reference?
I know full well that if theres independent information for each row LEFT JOIN is suitable, but where theres in this case only several ids to reference for many rows, i just am not clicking with how i could get it to work...
The current way i am achieving my desired result in PHP/MySQL
$itemid = $row['item_id'];
$secid = mysql_query(" SELECT * FROM item_groups WHERE item_id='$itemid' ");
while ($secidrow = mysql_fetch_assoc($secid)) {
//echo $secidrow["section_id"]; //testing
$id = $secidrow["section_id"];
$secnameget = mysql_query(" SELECT * FROM items_section_list WHERE item_sec_id='$id' ");
while ($secname = mysql_fetch_assoc($secnameget)) {
echo $secname["section_name"];
}
}
Example of the data
Item groups
:drink
:food
:shelf
Item List
itemId, groupId
Group List
groupId, groupTitle
The idea so outputting data to a table instead of outputting "Item & Id Number, in place of the ID Number the title actually appears.
I have achieved the desired result but i am always interested in seeking better ways to achieve the desired result.
If I've deciphered your code properly, you should be able to use the following query to get both values at the same time.
$itemid = $row['item_id'];
$secid = mysql_query("
SELECT *
FROM item_groups
LEFT JOIN items_section_list
ON items_section_list.item_sec_id = item_groups.section_id
WHERE item_id='$itemid'
");
while ($secidrow = mysql_fetch_assoc($secid)) {
//$id = $secidrow["section_id"];
echo $secidrow["section_name"];
}

How to Output the results of a MySQL query that used aliases?

I have two primary MySQL tables (profiles and contacts) with many supplementary tables (prefixed by prm_). They are accessed and manipulated via PHP.
In this instance I am querying the profiles table where I will retrieve an Owner ID and a Breeder ID. This will then be referenced against the contacts table where the information on the Owners and Breeders is kept.
I received great help here on another question regarding joins and aliases, where I was also furnished with the following query. Unfortunately, I am having huge difficulty in actually echoing out the results. Every single site that deals with Self Joins and Aliases provide lovely examples of the queries - but then skip to "and this Outputs etc etc etc". How does it output????
SELECT *
FROM (
SELECT *
FROM profiles
INNER JOIN prm_breedgender
ON profiles.ProfileGenderID = prm_breedgender.BreedGenderID
LEFT JOIN contacts ownerContact
ON profiles.ProfileOwnerID = ownerContact.ContactID
LEFT JOIN prm_breedcolour
ON profiles.ProfileAdultColourID = prm_breedcolour.BreedColourID
LEFT JOIN prm_breedcolourmodifier
ON profiles.ProfileColourModifierID = prm_breedcolourmodifier.BreedColourModifierID
) ilv LEFT JOIN contacts breederContact
ON ilv.ProfileBreederID = breederContact.ContactID
WHERE ProfileName != 'Unknown'
ORDER BY ilv.ProfileGenderID, ilv.ProfileName ASC $limit
Coupled with this is the following PHP:
$owner = ($row['ownerContact.ContactFirstName'] . ' ' . $row['ownerContact.ContactLastName']);
$breeder = ($row['breederContact.ContactFirstName'] . ' ' . $row['breederContact.ContactLastName']);
All details EXCEPT the contacts (gender, colour, etc.) return fine. The $owner and $breeder variables are empty.
Any help in settling this for me would be massively appreciated.
EDIT: My final WORKING query:
SELECT ProfileOwnerID, ProfileBreederID,
ProfileGenderID, ProfileAdultColourID, ProfileColourModifierID, ProfileYearOfBirth,
ProfileYearOfDeath, ProfileLocalRegNumber, ProfileName,
owner.ContactFirstName AS owner_fname, owner.ContactLastName AS owner_lname,
breeder.ContactFirstName AS breeder_fname, breeder.ContactLastName AS breeder_lname,
BreedGender, BreedColour, BreedColourModifier
FROM profiles
LEFT JOIN contacts AS owner
ON ProfileOwnerID = owner.ContactID
LEFT JOIN contacts AS breeder
ON ProfileBreederID = breeder.ContactID
LEFT JOIN prm_breedgender
ON ProfileGenderID = prm_breedgender.BreedGenderID
LEFT JOIN prm_breedcolour
ON ProfileAdultColourID = prm_breedcolour.BreedColourID
LEFT JOIN prm_breedcolourmodifier
ON ProfileColourModifierID = prm_breedcolourmodifier.BreedColourModifierID
WHERE ProfileName != 'Unknown'
ORDER BY ProfileGenderID, ProfileName ASC $limit
Which I could then output by:
$owner = ($row['owner_lname'] . ' - ' . $row['owner_fname']);
Many Thanks to All!
I guess you're using the mysql_fetch_array or the mysql_fetch_assoc-functions to get the array from the result-set?
In this case, you can't use
$row['ownerContact.ContactFirstName']
as the PHP-Docs read:
If two or more columns of the result have the same field names, the
last column will take precedence. To access the other column(s) of the
same name, you must use the numeric index of the column or make an
alias for the column. For aliased columns, you cannot access the
contents with the original column name.
So, you can either use an AS in your SQL-query to set other names for the doubled rows or use the numbered indexes to access them.
This could then look like this:
Using AS in your Query
In your standard SQL-query, the columns in the result-set are named like the columns which their values come from. Sometimes, this can be a problem due to a naming-conflict. Using the AS-command in your query, you can rename a column in the result-set:
SELECT something AS "something_else"
FROM your_table
This will rename the something-column to something_else (you can leave the ""-quotes out, but I think it makes it more readable).
Using the column-indexes for the array
The other way to go is using the column-index instead of their names. Look at this query:
SELECT first_name, last_name
FROM some_table
The result-set will contain two columns, 0 ==> first_name and 1 ==> last_name. You can use this numbers to access the column in your result-set:
$row[0] // would be the "first_name"-column
$row[1] // would be the "last_name"-column
To be able to use the column-index, you'll need to use mysql_fetch_row or the mysql_fetch_assoc-function, which offers an associative array, a numeric array, or both ("both" is standard).
you need to replace the * with the data you need , and the similar ones you have to make aliases too :
ownerContact.ContactFirstName as owner_ContactFirstName
and
breederContact.ContactFirstName as breeder_ContactFirstName .
like this :
select ownerContact.ContactFirstName as owner_ContactFirstName , breederContact.ContactFirstName as breeder_ContactFirstName from profiles join ownerContact ... etc
in this way you will write :
$owner = ($row['owner_ContactFirstName'] . ' ' . $row['owner_ContactLastName']);
$breeder = ($row['breeder_ContactFirstName'] . ' ' . $row['breeder_ContactLastName']);
You cannot specify table alias when you access row using php. Accessing it by $row['ContactFirstName'] would work if you didn't have 2 fields with the same name. In this case whatever ContactFirstName appears second overwrites the first.
Change your query to use fields aliases, so you can do $owner = $row['Owner_ContactFirstName'].
Another option I'm not 100% sure is to access field by index, not by name(e.g. $owner=$row[11]). Even if it works I don't recommend to do so, you will have a lot of troubles if change your query a bit.
On outer select You have only two tables:
(inner select) as ilv
contacts as breederContact
there is no ownerContact at all

Limit SQL join when using CodeIgniter active record class

I'm getting a product listing. Each product may have 1 or more image, I only want to return the first image.
$this->db->select('p.product_id, p.product_name i.img_name, i.img_ext');
$this->db->join('products_images i', 'i.product_id = p.product_id', 'left');
$query = $this->db->get('products p');
Is there anyway to limit the db->join to 1 record using the CI active record class?
Add $this->db->limit(1); before calling $this->db->get('products p');. See the docs at ellislab.com: search the page for limit.
EDIT: I misread the fact that you were trying to apply the LIMIT to the internal JOIN statement.
No. Since you can not do a LIMIT on an internal JOIN statement in regular SQL you can not do it with Code Igniter's ActiveRecord class.
You can achieve what you want using $this->db->group_by with a left join:
$this->db->select('products.id, products.product_name, products_images.img_name, products_images.img_ext');
$this->db->from('products');
$this->db->join('products_images', 'products_images.product_id = products.id', 'left');
$this->db->group_by('products.id');
$query = $this->db->get();
This should give you results by products.id (without repetition of products), with the first matching record from products_images joined to each result row. If there's no matching row from the joined table (i.e. if an image is missing) you'll get null values for the products_images fields but will still see a result from the products table.
To expand on #Femi's answer:
There's no good way to limit the JOIN, and, in fact, you don't really want to. Assuming both products_image.product_id and products.id have indexes (and they absolutely should if you're going to join against them repeatedly) when the database engine does a join, it uses the indexes to determine what rows it needs to fetch. Then the engine uses the results to determine where on the disk to find the records it needs. If you
You should be able to see the difference by running these SQL statements:
EXPLAIN
SELECT p.product_id, p.product_name, i.img_name, i.img_ext
FROM products p
LEFT JOIN products_images i
ON i.product_id = p.product_id
as opposed to:
EXPLAIN
SELECT p.product_id, p.product_name, i.img_name, i.img_ext
FROM (SELECT product_id, product_name FROM products) p
LEFT JOIN (SELECT img_name, img_ext FROM products_images) i
ON i.product_id = p.product_id
The first query should have an index, the second one will not. There should be a performance difference if there's a significant number of rows the the DB.
Had this issue too the way I solved it was iterating over the results and removing the current object if the product_id had existed in a previous one. Create a array, push the product_id's to it while checking if they are repeats.
$product_array = array();
$i = 0;
foreach($result as $r){
if(in_array($r->product_id,$product_array)){
unset($result[$i]);
}else{
array_push($product_array,$r->product_id);
}
$i++;
}
$result = array_values($result); //re-index result array
Now $result is what we want

Categories