I am using this to get customer name for auto complete function.
$query = $db->query("SELECT orderr_customer_name FROM orderr WHERE orderr_customer_name LIKE '$queryString%' GROUP by orderr_customer_name LIMIT 10");
if($query) {
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\''.$result->orderr_customer_name.'\');">'.$result->orderr_customer_name.'</li>';
How can i Select another table? this is the table i want to use
("SELECT customer_name FROM customer WHERE customer_name LIKE '$queryString%' GROUP by customer_name LIMIT 10");
Thanks alot.
You should look into something like that (Note that I can't test it now, this is only a guideline):
SELECT orderr.orderr_customer_name, customer.customer_name
FROM customer INNER JOIN orderr_customer_name ON orderr.orderr_customer_name = customer.customer_name
WHERE customer_name LIKE '$queryString%' GROUP by customer_name LIMIT 1
Pleasy read the doc: Here
It sounds like you're asking how to query and combine the results from two tables through the use of a single query. This can be accomplished rather easily with a UNION in MySQL.
(SELECT orderr_customer_name AS customer_name FROM orderr WHERE orderr_customer_name LIKE '$queryString%')
UNION
(SELECT customer_name FROM customer WHERE customer_name LIKE '$queryString%')
ORDER BY customer_name LIMIT 10
Don't forget to make sure you escape/sanitize $queryString, and that you have proper indexes on your tables. Also, in your specific example, it appears you might be duplicating customer names in at least one place; while some applications require this, you may also want to consider normalization - but that's another topic.
Related
I'm trying to have my query count the rows and have it return the most common name in that list, then from that it counts how many times that name appears and outputs the name and the amount of times its there
This is the code I'm using:
$vvsql = "SELECT * FROM votes WHERE sid=? ORDER BY COUNT(*) DESC LIMIT 1";
$vvresult = $db->prepare($vvsql);
$vvresult->execute(array($_GET['id']));
$vvcount = $vvresult->rowCount();
foreach ($vvresult->fetchAll(PDO::FETCH_ASSOC) as $row) {
echo $row['username'];
echo $vvcount;
}
However, it just displays the first username in the table and counts up the entire table. I'm pretty new to this so I'm sorry if this is a bad post or if it didn't make much sense.
You would seem to want:
SELECT name, COUNT(*) as cnt
FROM votes
GROUP BY name
ORDER BY COUNT(*) DESC
LIMIT 1;
Note the GROUP BY. You may also want to filter by sid but your question makes no mention of that.
select username, count(*) as c
FROM votes
GROUP BY username
ORDER BY c DESC
I would like to get number of all records and get last record :
$sql_count_sms = "SELECT count(*) as total,content,id FROM android_users_sms WHERE user_id=$id ORDER BY id DESC";
$result_count_sms = mysql_query($sql_count_sms);
$row_num_sms = mysql_fetch_assoc($result_count_sms);
$num_sms = $row_num_sms['total'];
$last_my_sms = $row_num_sms['content'];
I can get number of records but I can't get last content record .
It returns first record !
Where is my wrong ?
Below codes works fine, but I think count(*) is faster than mysql_num_rows .
$sql_count_sms = "SELECT content,id FROM android_users_sms WHERE user_id=$id ORDER BY id DESC";
$result_count_sms = mysql_query($sql_count_sms);
$row_num_sms = mysql_fetch_assoc($result_count_sms);
$num_sms = mysql_num_rows($result_count_sms);
$last_my_sms = $row_num_sms['content'];
Any solution?
The grain of the two results you want is not the same. Without using a sub-query you can't combine an aggregate and a single row into the same result.
Think of the grain as the base unit of the result. The use of GROUP BY and aggregate functions can influence that "grain"... one result row per row on table, or is it grouped by user_id etc... Think of an aggregate function as a form of grouping.
You could break it out into two separate statements:
SELECT count(*) as total FROM android_users_sms WHERE user_id = :id;
SELECT * FROM android_users_sms WHERE user_id = :id ORDER BY id DESC LIMIT 1;
Also, specific to your question, you probably want a LIMIT 1 in combination with the ORDER BY to get just the last row.
Now, counter intuitively perhaps, this should also work:
SELECT count(*), content, id
FROM android_users_sms
WHERE user_id = :id
GROUP BY id, content
ORDER BY id
LIMIT 1;`
This is because we've changed the "grain" with the GROUP BY. This is the real nuance and I feel like this could probably be explained better than I am doing now.
You could also do this with a sub query like so:
SELECT aus.*,
(SELECT count(*) as total FROM android_users_sms WHERE user_id = :id) AS s1
FROM android_users_sms AS aus
WHERE user_id = :id ORDER BY id DESC LIMIT 1;
Look at my code, I want the select statement order by the count percentage after I fetch the data from this select statement, obviously, it's not logical. What can I do? Help, appreciate.
<?php
//myslq connection code, remove it because it's not relate to this question
$stm =$db->prepare("SELECT id ,term_count, COUNT(user_id) as count FROM sign WHERE term IN (:term_0,:term_1) GROUP BY user_id ORDER by count DESC");
//trying replace order by count with $combine_count, but it's wrong
$term_0="$term[0]";
$term_1="$term[1]";
$stm->bindParam(":term_0", $term_0);
$stm->bindParam(":term_1", $term_1);
stm->execute();
$rows = $stm->fetchALL(PDO::FETCH_ASSOC);
foreach ($rows as rows) {
$count=$rows['count'];
$term_count_number=$rows['term_count'];
$count_percentage=round(($count/$count_user_diff)*100);
$count_key_match=round(($count/$term_count_number)*100);
$combine_count=round(($count_percentage+$count_key_match)/2);
//issue is here, I want the select statement order by $combine_count
}
?>
SELECT id ,term_count, COUNT(user_id) as `count`
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY user_id
ORDER by `count` DESC");
Since "count" is a function, it would be better to put backtics around the non-function "counts", as done above.
GROUP BY should list the field not aggregated. Otherwise, it does not know which id and term_count to fetch. So, depending on what you are looking for,
Either do
SELECT user_id, COUNT(*) as `count` -- I changed this line
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY user_id
ORDER by `count` DESC");
or do
SELECT id ,term_count, COUNT(*) as `count`
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY id ,term_count -- I changed this line
ORDER by `count` DESC");
SQL Syntax Logic
SELECT column1, count(column1) AS amount
FROM table_name
GROUP BY column1
ORDER BY amount DESC
LIMIT 12
I done my project in Yii. Then i wants to display the values related items which i showing.
i wanna display related recipes like cuisine and course field from the recipe table.
i show my condition here. how to write the query for to display the two field based on the related items.
$query="SELECT * FROM recipe where cuisinename, course_id LIKE '%$cuisine%' AND course_id LIKE '%$course%' ORDER BY recipe_id DESC LIMIT 4";
then i tried this code also:
$query="SELECT * FROM recipe where type LIKE '%$type%' ORDER BY recipe_id DESC LIMIT 4";
its working but i wants to display both field values to be related.
Try this
$query="SELECT * FROM recipe where cuisinename LIKE '%$cuisine%' AND course_id LIKE '%$course%' ORDER BY recipe_id DESC LIMIT 4";
Your first query syntax is not correct, if you want to use 2 conditions as you are trying to apply in your first query then use below query:
SELECT * FROM recipe WHERE cuisinename LIKE '%$cuisine%' AND course_id LIKE '%$course%' ORDER BY recipe_id DESC LIMIT 4;
But if you want to add 3rd condition also which is in your 2nd query then use below query:
SELECT * FROM recipe WHERE cuisinename LIKE '%$cuisine%' AND course_id LIKE '%$course%' AND `type` LIKE '%$type%' ORDER BY recipe_id DESC LIMIT 4;
If you want something else then please elaborate your question.
I have such sql:
mysql_query("SELECT *
FROM car
LEFT JOIN client
ON car.CodeClient = client.Code
LEFT JOIN telephone
ON car.CodeClient = telephone.CodeClient
WHERE Marka Like '$Marka'
and Model Like '%$Model%'
and EngineVol Like '%$EngineVol%'
and EngineType Like '%$EngineType%'
and DateMade Like '%$DateMade%'
");
And I need to select CodeClient and TelephoneNumber from telephone table, but select first entry for every Client, not all telephone, but first. Grouping is not the solving!
wouldn't this work? Sorry if it's not what you're looking for, but the question is a little unclear:
ORDER BY car.id LIMIT 1
or just:
LIMIT 1