Is there anyway to get the seed fieldname?
What I mean by seed is the field that's been created with something like this;
INT NOT NULL AUTO_INCREMENT PRIMARY KEY
I plan on using this ( getting the seed field name pro grammatically ) in coming up with the fastest SQL query to get the number of records in a table.
The function I plan on writing is something like this. Please fill in the blanks and provide the getSeed function inner mechanics.
function get_record_count ($dbh,$table,$where){
//get the seedfield name in the {table} programmatically
$seed = getSeed($dbh,$table);
$sql = "select count({$seed}) as `count` from {$table} " . $where;
//do the mysql query & get num rows to return it...
}
If you're not trying to count the number of non-NULL values in the column (COUNT(expr) doesn't count NULLs), then just use SELECT COUNT(*) and let MySQL use the same index that is used in the WHERE clause to answer COUNT(*).
$sql = "SELECT COUNT(*) AS `count` FROM {$table} " . $where;
Related
I am new to SQL and i want to copy distinct dates from a column named starttime in a table user in a database MyDb, to a column playing_date in a table collection in another database named viral.
I have used the following query but its not working:
mysqli_select_db($con,'MyDb');
$query1 = "INSERT INTO viral.collection.playing_date SELECT DISTICT date(starttime) FROM user";
if(mysqli_query($con,$query1))
echo "Successfully Inserted";
else
echo "error";
How should i correct it?
You are missing the N in DISTINCT
INSERT INTO viral.collection.playing_date SELECT DISTINCT date(starttime) FROM user
SELECT DISTICT date(starttime) into viral.collection.playing_date FROM user;
this will work!
this is my query, and I already know the problem but I dont have an idea about how to solve it:
$queryString = "SELECT * FROM `users_list` "WHERE `user_id` like (SELECT `user_id` FROM `equipments_list` WHERE `equipment_mac` like '%$searchStringMacRevised%')"
So, this is the error sometimes I get:
Subquery returns more than 1 row
I see that the problem is that if the mac address is registered more than once, it will give me more than one user ID, and when we are going to select the information, I have too much user id to generate the table. Can you guys help me to see how can I solve this problem?
Since you are just comparing to the user id directly, you could use an IN clause, such as
SELECT * FROM users_list
WHERE user_id IN
(SELECT user_id FROM equipments_list
WHERE equipment_mac like '%$searchStringMacRevised%')
This would allow you to potentially compare to multiple user ids.
If we want only 1 user id, then you may need to use the LIMIT type of query suggested in other answers.
It means that your inner select is returning more than one row , it should exactly return 1 row in order to match record for outer query
$queryString = "SELECT * FROM `users_list` "WHERE `user_id` like (SELECT `user_id` FROM `equipments_list` WHERE `equipment_mac` like '%$searchStringMacRevised%' LIMIT 1)"
Let's say we have a record in table 'orders' with id=1. This query:
SELECT * FROM 'orders' WHERE id = 'abc1'
won't return mentioned record. But this query:
SELECT * FROM 'orders' WHERE id = '1abc'
will return the record with id=1. This is because when MySQL converts string to number 'abc1' becomes 0, but '1abc' becomes 1. Is there a nice way to make MySQL search strictly for records with id from query, i.e. not return the record with id=1 in both mentioned cases?
What about using :
SELECT * FROM 'orders' WHERE id LIKE '1abc' COLLATE utf8_bin
or even
SELECT * FROM 'orders' WHERE STRCMP(id, '1abc') = 0;
I handled it with PHP before submitting the SQL query
$idTest = '1abc';
if (is_numeric($id){
$query = "SELECT * FROM 'orders' WHERE id = '$idTest'"
}
This will prevent submitting queries if the $idTest has a string
I have an existing mysql query that I need to add to and I'm not sure how to go about it.
Here is my current sql query.
SELECT tbl_brokerage_names.brokerage_id, tbl_brokerage_names.short_name,
b.indication, b.max_indication
FROM tbl_brokerage_names
LEFT JOIN (
SELECT * FROM tbl_recommendation_brokerages
WHERE recommendation_id = {$_GET['id']}
) b ON (tbl_brokerage_names.brokerage_id = b.brokerage_id)
ORDER BY tbl_brokerage_names.short_name ASC
Here is the query that I need to work into the previous query.
SELECT * , COUNT( * )
FROM tbl_streetaccounts
JOIN tbl_brokerage_names
WHERE tbl_brokerage_names.brokerage_id = tbl_streetaccounts.brokerage_id
Basically I need to return a count, so I need to combine these two queries.
You should run these as two separate queries.
The COUNT(*) query will return a single row, so there's no way to "combine" it with the first query while preserving the multi-row result of the first query.
Also, when you SELECT *, COUNT(*) you will get columns from some arbitrary row.
By the way, you have a glaring SQL injection vulnerability. Don't interpolate $_GET parameters directly in your SQL query. Instead, coerce it to an integer:
<?php
$id = (int) $_GET['id'];
$sql = "SELECT ... WHERE recommendation_id = {$id}";
Like #Bill said, you cannot get the count in every row without really weird syntax, but you can get an overall count using GROUP BY ... WITH ROLLUP.
e.g.:
<?php
$id = mysql_real_escape_string($_GET['id']); //works with anything, not just numbers
$query = "
SELECT tbl_brokerage_names.brokerage_id
, tbl_brokerage_names.short_name
, b.indication
, b.max_indication
, count(*) as rowcount
FROM tbl_brokerage_names
LEFT JOIN (
SELECT * FROM tbl_recommendation_brokerages
WHERE recommendation_id = '$id' //The single quotes are essential for safety!
) b ON (tbl_brokerage_names.brokerage_id = b.brokerage_id)
GROUP BY tbl_brokerage_names.brokerage_id WITH ROLLUP
ORDER BY tbl_brokerage_names.short_name ASC
";
The GROUP BY .. WITH ROLLUP will add an extra line to the result with all NULL's for the non aggregated columns and a grand total count.
If you have any lines where rowcount > 0 then you need to add extra clauses from table b to the group by clause to prevent MySQL from hiding arbitrary rows.
Table tbl_brokerage_names is already fully defined because you are grouping by the primary key.
If i limit query1 to 100k this script will create a new table, but if I try and transfer all 300k entries it fails without error. any suggestions?
$query1 = "SELECT DISTINCT city, region FROM geolocations";
$result = mysql_query($query1);
while ($row = mysql_fetch_assoc($result)) {
//create array of ids to select from DB
$id_arr[] = $row['location_id'];
}
mysql_free_result($result);
//join the array to create SELECT statement
$ids = join(',',$id_arr);
//create new table using data associated with locations_ids in id_arr
$query2 = "CREATE TABLE clean_gls SELECT * FROM geolocations WHERE location_id IN ($ids) ";
mysql_query($query2);
As Mat said, there appears to be something missing from your code, but you should really look into using a join or subquery to generate your array of location ids, so that all the processing is done by the server.
EDIT:
So, I still don't understand how you're getting location_id from your original query, but maybe this will do what you want:
CREATE TABLE clean_gls (PRIMARY KEY (city, region)) IGNORE SELECT * FROM geolocations
If IGNORE isn't what you want, try REPLACE.
As mentioned before, you have issues with your original code:
You are not selecting location_id, so you cannot create an array of
location_id
Why are you pulling all these ids into php only to pass
them back to mysql?
If you are trying to put distinct city and region combinations into your new table then do it with a single database execution similar to the following:
CREATE TABLE clean_gls
SELECT DISTINCT city, region
FROM geolocations
This query worked I wanted to select distinct city, region combinations and group it with its associated data in the new table.
CREATE TABLE clean_gls SELECT * FROM geolocations GROUP BY city, region;