All,
Say I have the following data set:
id docId meta_key meta_value
1 356 rating 2.0
2 356 total_votes 10
3 356 total_rating 200
Basically I'm trying to pull all the values in my meta_value column by selecting the docId of 356 and only selecting the meta_key rows that I have listed.
Any ideas how to do this in a single query?
EDIT: My desired output would be something like this:
rating total_votes total_rating
2.0 10 200
I'm trying to get it so that I can use these values with a mysql_fetch_array in PHP.
Thanks!
What you are asking for is a pivot table. Unfortunately, MySQL doesn't provide functions for generating pivot tables.
It's still doable, but with more than two columns, it starts to get messy.
I would loop through the records in PHP and build the array.
To borrow slightly from Ghost Developer's answer:
$assoc_array = array();
$query = "SELECT meta_key, meta_value FROM table WHERE docId=356";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_assoc($result)) {
$assoc_array[$row['meta_key']] = $row['meta_value'];
}
mysqli_free_result($result);
All columns from all rows for docId 356
SELECT * FROM mytable WHERE docId = 356;
Only the meta_value column from all rows for docId 356
SELECT meta_value FROM mytable WHERE docId = 356;
Get all values as a comma-separated list for docId 356
SELECT GROUP_CONCAT(meta_value) meta_values FROM mytable WHERE docId = 356;
$query = mysql_query("SELECT * FROM table WHERE docId=356");
while ($result = mysql_fetch_array($query)) {
$metaKey = $result['meta_key'];
$meta_value = $result['meta_value'];
echo $meta_key.' : '.$meta_value.'<br>';
}
Related
I would like to get lowest price of product based on last crawled dates by various resellers. My current function is very basic, it gets me lowest price from table without considering reseller ids and crawled timestamps.
I've rough idea that we can SELECT * FROM "custom_data_table" and process the data using php. Please have a look at attachment for further clarification.
function get_lowest_price($table_id) {
global $wpdb;
$table_prices = $wpdb->get_results(
$wpdb->prepare(
"SELECT price FROM `custom_data_table` WHERE tableid= %d"
,$table_id)
);
if (!empty($table_prices) && $table_prices !== NULL)
return rtrim(min($table_prices)->price, '00');
}
The right query here is:
SELECT price
FROM custom_data_name cdn, (
SELECT MAX(crawled) AS maxCrawled, resellerid
FROM custom_data_name
GROUP BY resellerid
) cdnFiltered
WHERE cdn.crawled = cdnFiltered.maxCrawled AND
cdn.resellerid = cdnFiltered.resellerid AND
tableid = %d;
Try this:
SELECT B.price
FROM (SELECT resellerid, MAX(crawled) max_crawled
FROM custom_data_table
GROUP BY resellerid) A
JOIN custom_data_table B
ON A.resellerid=B.resellerid AND A.max_crawled=B.crawled;
Maybe use ORDER BY crawled and LIMIT 1
I want to get a value from a table like that:
SELECT id_image FROM `ps_image` WHERE id_product = 903 limit 1
How can i integrate this sql command in prestashop?
$product_id = '903';
$query = Db:: getInstance()->getRow(
'SELECT id_image FROM '._ DB_PREFIX_.'image WHERE id_product ='. $product_id
);
Use $query variable to get id of image.
-----ID
1
5
1,6
3
4
1,36
1
I have these '1,6,36' number to search.
It should find any row that contains 1 OR 6 OR 36 (e.g. total 4 rows in the above table)
I tried
FIND_IN_SET(ID, '1,6,36')
ID IN (1,6,36)
None of them worked.
Any idea how to achieve this ?
FIND_IN_SET(1, ID) OR
FIND_IN_SET(6, ID) OR
FIND_IN_SET(36, ID)
You have to do a sql query called IN, and you should do it as follows:
$id_array = array(1, 6, 36)
$sql = "SELECT * FROM table WHERE id IN ($id_array)";
$search = mysqli_query($sql_connection, $sql);
Then to display, you have to do a PHP loop like follows:
while($ids = mysqli_fetch_array($search)) {
echo '<h3>'.$ids['field_from_db'].'</h3><br />';
}
I want to write a select query where i user have to pass two input ID and SourceID.
But here is the twist, Input ID is mandetory and SourceID is Optional. I want to write a select query where if a user passes only Input ID then my select query will filter data based on ID but if user passes both ID and SourceID then user should Get Data based on Both Filters.
I am pretty much new to database query so i am not able to figure out how to do this?
My request body:
<body>
<p:GetEntryByID xmlns:p="http://abcFarm.org/">
<!--Exactly 1 occurrence-->
<xs:ExistingID xmlns:xs="http://abcFarm.org/">?</xs:ExistingID>
<!--0 to 1 occurrence-->
<xs:TargetSourceID xmlns:xs="http://abcFarm.org/">?</xs:TargetSourceID>
</p:GetEntryByID >
</body>
I have written a sql query which is not working as:
select * from entry WHERE ID='ID1' AND (e.SourceID='SourceID1' Or ID='ID1');
But this is not fulfilling my above need.
Please Help.Thanks in advance.
Try this,
SELECT *
FROM entry
WHERE ID = 'ID1' AND
SourceID = COALESCE('SourceID1', SourceID)
What it means is that when NULL is passed in SourceID1, the value that will be compared with column SourceID is the value of the column itself.
$id = "Your_ID1";
$SourceID1 = "Your_SourceID1";
if($id){
$query = "select * from entry WHERE ID=$id";
if($SourceID1){
$query .= " AND SourceID = $SourceID1";
}
$query .= ";";
}
I have a MySQL script like this: SELECT id, name FROM users WHERE id IN (6,4,34)
The sequence in the IN(...) array is very important. Is it possible to get them in the given sequence?
You can use the MySQL FIELD function to keep it compact;
SELECT id, name
FROM users
WHERE id IN (6, 4, 34)
ORDER BY FIELD(id, 6, 4, 34);
Try
SELECT id, name FROM users WHERE id IN (6,4,34) order by FIELD(id,6,4,34)
You can use any expression in the ORDER BY clause, including a 'CASE':
ORDER BY CASE id
WHEN 6 THEN 1
WHEN 4 THEN 2
WHEN 34 THEN 3
END ASC
If your list comes from the application programming layer, you might build this with the following (PHP here):
$sortVal = 1;
foreach($ids as $id_val) {
$cases[] = sprintf('WHEN %i THEN %i', $id_val, $sortVal++);
}
$order_by = 'ORDER BY CASE id ' . implode($cases) . ' END ASC';
However, I'll mention that Joachim's answer is quite elegant :-)
A complete example based on Chris Trahey answer.
$ids = array("table1", "table2", "table3");
$sortVal = 1;
foreach ($ids as $id_val) {
$cases[] = sprintf("WHEN '%s' THEN %u ", $id_val, $sortVal++);
}
$order_by = 'ORDER BY CASE `tableName` ' . implode($cases) . ' END ASC';
$result = mysqli_query( $con, "
SELECT DISTINCT tableName
FROM `table`
$order_by");