Dear Sir/Mam iam trying to sort duplicates from a table.
I want to check if a name has teh same streetname and then only show the first result of those, resulting in unique names ommitting the doubles.
I tried the distinct(name) and the group by statements but to no avail.
Group by ended up limmiting my results
$klantquery = "SELECT name, ID, street, tel, email FROM customers where name LIKE '%$search%' ORDER BY name ASC";
This query works but shows all records i need to sift out the extra ones? in order to show only unique results.
iam using mysql and php
So now i used this query:
$klantquery = "SELECT DISTINCT naam,straat,email,huisnummer,plaats,date FROM ".$GLOBALS["klanten"]." where naam LIKE '%$klantsearch%' ORDER BY naam ASC";
Works like a charm but it omits the ID row in the results so its useless??
How do include the ID field in the results?
Where is the logic?
Got it first i selected the records i needed with distinct then later added the IDs with a subquery, elegant not realy, works though.
$klantquery = "SELECT Distinct naam,straat,email,huisnummer,plaats,date FROM ".$GLOBALS["klanten"]." where naam LIKE '%$klantsearch%' ORDER BY naam ASC";
while ($result=$klantpaging->result_assoc()) {
$subquery = "SELECT ID,naam,straat FROM ".$GLOBALS["klanten"]." where naam='".addslashes($result['naam'])."' AND straat='".$result['straat']."'";
$subresult = mysql_query($subquery) or die("Query failed : " . mysql_error());
while ($subline = mysql_fetch_assoc($subresult)) {
$result[ID] = $subline[ID];
}
$klantrecords[] = $result;
}
Related
I have a table named info which has 3 columns: Id, city, country.
I need to filter the table. That is, when i search for city and country it should show me the exact row but when i will search only for the country it should show all the cities in that country. how can i write the condition?
How it is looking right now
I have tried using AND and OR but they only do one task. my current query
SELECT * FROM info WHERE name='$_POST[name1]' AND country='$_POST[country1]';
this work for both fields and
SELECT * FROM info where name='$_POST[name1]' OR country='$_POST[country1]';
this fails when searched on both fields at once
Try this :
$condition = '';
$query = ''
if(!empty($_POST[name1])) { $condition .= "and name LIKE '$_POST[name1]' "; }
if(!empty($_POST[country1])){ $condition .= "and country LIKE '$_POST[country1]' "; }
$query = "SELECT * FROM info WHERE Id !='' " . $condition;
mysqli_query($connection,$query);
Both name and country are varchar so use LIKE in this way:
SELECT * FROM info WHERE name LIKE '$_POST[name1]' OR country LIKE '$_POST[country1]';
try this.
I want write search query which fetch the data from students table based on multiple conditions like by ID , by name and by date of birth like that.
if I use OR condition like
Select * from students where Id='101' or name='Kumar' or age='21';
it will return if any one field is entered. If multiple fields are entered it will consider first only.
If I use AND condition like
Select * from students where Id='101' and name='Kumar' and age='21';
It will return only if all fields are entered. If any one of the field is empty it will return zero, I mean it will return empty. I want to get result even some fields are empty.
I'm using this quarry for swing application if I haven't enter text in one of the field then it will become empty like I'd='' not null
How to get Result at this condition
Select * from students where Id='' and name='' and age='21'
I want to get result even some fields are empty.
Then include that in your conditions. Something like this:
WHERE
(Id = '101' OR Id IS NULL) AND
(name = 'Kumar' OR name IS NULL) AND
(age = '21' OR age IS NULL)
Nesting conditions in parentheses allows you to build fairly complex conditional logic. How you define that logic for finding the records you want to find is really up to you.
Use LIKE clause without %s. So it'll be acting as a simple =. Here's how it will work.
if(txtId.getText().equals("")){
String id = "%%";
}else{
String id = txtId.getText();
}
if(txtName.getText().equals("")){
String name = "%%";
}else{
String name = txtName.getText();
}
if(txtAge.getText().equals("")){
String age = "%%";
}else{
String age = txtAge.getText();
}
resultset = statement.executeQuery("SELECT * FROM student WHERE id LIKE '"+ id +"' and name LIKE '"+ name +"' and age LIKE '"+ age +"';");
Try this out. It'll work like a charm. (It's up to you to change the query according to your database)
I want to select data from my table people (MySQL) but only where friend is not empty:
$sql = "SELECT * FROM people WHERE friend = '$friend' ORDER BY id ASC AND WHERE friend IS NOT NULL;" ;
I tried to write it like this, but it is not working. In this case it is selecting nothing.
Your SQL was malformed:
$sql = "SELECT * FROM people
WHERE friend = '$friend' AND friend != ''
AND friend IS NOT NULL
ORDER BY id ASC";
With that said it sounds like you don't want to query at all when $friend has no value:
if ($friend) {
$sql =... etc.
} else {
// No query, $friend had no value
}
You really want to insure your DB is as clean as possible (assuming you can at this point, and insure that you aren't inserting empty strings.
SELECT * FROM people WHERE friend!='' ORDER BY id ASC
I have these three queries, that I need to combine into one.
$sql = "SELECT SUM(datamb) AS value_sum FROM maindata GROUP BY phonenumber";
$sql1 = "select dataplan as currentplan from maindata GROUP BY phonenumber";
$sql2 = "SELECT DISTINCT phonenumber AS value_sum1 FROM maindata";
So I can display them in three columns like this:
while ($row = mysql_fetch_assoc($result)){
echo "<TABLE id='display'>";
echo "<td><b>Data Usage This Period: ". ROUND ($row["value_sum"],2) . "MB</b></td> ";
echo "<td><b>Data Plan: ". $row["currentplan"] . "</b></td> ";
echo "<td><b>Phone Number: ". $row["phonenumber"] . "</b></td> ";
echo "</TABLE>";
}
I have used UNION which just makes one big column with all the data correct which is close, but I need them to all display in one table in three columns. Thank you for any thoughts, if you thought is I should punch myself in the face, already done :)
Use the GROUP BY keyword to create distinct records and sum over the remaining values.
SELECT phonenumber,
dataplan AS currentplan,
SUM(datamb) AS value_sum
FROM maindata
GROUP BY phonenumber, dataplan;
This query will give you unique combinations of phone number and data plan, and will sum the datamb field within these distinct groups. This is great practice for learning the GROUP BY keyword which is very useful for this kind of calculation.
EDIT: When you see a DISTINCT, that's one hint that you can also GROUP BY that field. It means there are likely duplicate values in that column or group of columns. Make sure there's a key defined for this specific group of fields with the same order as the ORDER BY clause for maximum efficiency.
illegal group by in $sql1
please inform yourself on the use of group by.
http://dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html
query would be
with illegal grouping:
select
SUM(datamb) AS value_sum,
dataplan as "first plan row value in this table for this phone number",
phonenumber AS value_sum1
FROM maindata
group by phonenumber
or without:
select
SUM(datamb) AS value_sum,
dataplan as currentplan ,
phonenumber AS value_sum1
FROM maindata
group by phonenumber, dataplan
I'm totally confused.
I run 2 the same queries, one with $wpdb, the other via mysql_query (also checked via phpmyadmin)
First query returns ONLY ONE row:
$wpdb->get_results("SELECT * FROM wp_terms WHERE slug LIKE '%info%' ORDER BY name ASC");
mysl_query returns 42 rows. The same result if run the query via phpmyadmin.
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
mysql_selectdb(DB_NAME,$con);
$res=mysql_query("SELECT * FROM wp_terms WHERE slug LIKE '%info%' ORDER BY name ASC",$con);
while($obj=mysql_fetch_object($res)) {
var_dump($obj);
}
How can it be ??
$wpdb->show_errors();
$wpdb->print_error();
show no errors.
Try:
$wpdb->get_results("SELECT * FROM $wpdb->terms WHERE slug LIKE '%info%' ORDER BY name ASC", ARRAY_A );
Do you have plugins that might interfere with the database?