I could use some assistance with an sql query. I have a small 3 column table, id, ip, and birthday. id auto increments.
I'm trying to select all birthdays that are associated with a specific ip, but I'm not sure if the SQL statement I wrote is correct. If someone could check this for me it would be appreciated.
$query = "SELECT birthday, COUNT(ip) FROM $table Group By ip HAVING ( COUNT(ip) > 1) WHERE ip=$ip";
To get all the birthdays with a specific IP you'd simply query:
SELECT birthday FROM table WHERE ip="desired value here"
If you're trying to count that information, then you could use:
SELECT ip, count(*) as `Total` FROM table WHERE id="desired value here" GROUP BY ip
Select birthday from $table where ip='<ip goes here>'
Be sure to properly escape your inputs. PDO or MySQLi prepared statements are advisable.
Related
I have two tables in a database. The first table is "tb_ctsreport" with fields "qr_id, idNum, date, time" and the other one is "tb_usersreg" with many fields which includes idNum, firstName, lastName, age and address. I have displayed my table using this query:
$query = "SELECT * FROM tb_ctsreport LEFT JOIN tb_usersreg ON tb_ctsreport.idNum=tb_usersreg.idNum";
This gives me a resulting table of qr_id, idNum, Name(concatenated firstName and lastName), date, time.
Then I wanted to create a search query from this table that I have created, however, I am lost and I don't know how will I include the values firstName and lastName when searching because it is placed in another table. This is my working query except for an undefined index for displaying the name since I don't know how.
$query = "SELECT * FROM tb_ctsreport WHERE CONCAT(qr_id, idNum, time, date) LIKE '%".$searchBox."%'";
I have also tried this sql query but only gives me an error.
$query = "SELECT * FROM tb_ctsreport WHERE CONCAT(qr_id, idNum, time, date) LIKE '%".$searchBox."%'
UNION
SELECT * FROM tb_usersreg WHERE CONCAT(lastName, firstName) LIKE '%".$searchBox."%'";
Please help me. I am just new to php. Thank you!
You can use a WHERE clause after the JOINs clauses. So you can write your SQL query as:
SELECT *
FROM tb_ctsreport
LEFT JOIN tb_usersreg ON tb_ctsreport.idNum = tb_usersreg.idNum
WHERE
CONCAT(
tb_ctsreport.qr_id,
tb_ctsreport.idNum,
tb_ctsreport.time,
tb_ctsreport.date,
tb_usersreg.lastName,
tb_usersreg.firstName
) LIKE :searchBox
In the above query :searchBox is your query parameter.
Care when you concatenate user input with your SQL query, this introduces a huge security vulnerability called SQL Injection. You shuld prefer to use parameterized query to avoid this issue.
When you are referencing multiple tables in an SQL query I advise you to always use the fully qualified name for the columns in order to avoid any ambiguity.
I currently have a table called cc_site, in this table I store the results of pings from different ip addresses.
The table has four fields:
auto increment ID field
date (which includes time)
IP address
status (used to determine if the ping was successful or not).
I'm trying to create a query which will give me the latest result based off the date field for an individual ip address. The code I'm currently using is below but it doesn't work unless the IP I'm using in the query is the latest entry in the table.
$query = "SELECT *
FROM cc_site
WHERE ip='$host'
AND Date IN (
SELECT max(date)
FROM cc_site
)";
I knew the query is incorrect however I have not been able to find code that would perform the query I need.
Any help would be great.
No need of that sub-query. Use can use ORDER and LIMIT instead -
SELECT * FROM cc_site WHERE ip='$host' ORDER BY `date` DESC LIMIT 1
This will sort the records in descending order according to date and return the record with highest record.
select * from cc_site
where ip='$host' and date in(select max(date) from cc_site where ip='$host');
I think this might give you the answer
I have several tables in my database such as
comments
status
events
I’m trying to create an SQL query procedure which counts data from these different tables based on the userID entered and then sum up the counts to create a unique valued. This is what i’ve tried so far but i’m having problems with the syntax. Where am I going wrong??
SELECT COUNT(user_id) AS comments FROM comment
WHERE user_id= userID
UNION ALL
SELECT COUNT(creator_id) AS events FROM event
WHERE creator_id=userID;
In a union, the fields are combined based on order. So giving the count field a different name in each part of the union does not make two fields. It becomes the same field in the end. To differentiate which value came from which table, add a hardcoded string literal like so:
SELECT COUNT(user_id) AS rows, 'comment' as tablename FROM comment
WHERE user_id= userID
UNION ALL
SELECT COUNT(creator_id) AS rows, 'event' as tablename FROM event
WHERE creator_id=userID;
I have a mySQL table which contains rows based on payments.
Each row contains their e-mail address, so I want to write a query using mySQL and PHP to find out how many people have 2 or more entries in this table.
This is basically to know if they have paid us more than once, by buying two packages, or renewing a subscription.
What would be the best way to do this?
select distinct email, count(*) as rowcount
from datatable
group by email
having rowcount > 1
EDIT: I wrote the above off the top of my head. A quick test on Oracle shows that the syntax is slightly wrong and should be
select distinct email, count(*) as rowcount
from datatable
group by email
having count(*) > 1
But this might just be Oracle not allowing the aliased column in the having clause. I'm not sure what mySQL would allow.
SELECT email, COUNT(email) as c_email FROM emails_table WHERE c_email > 1;
With PHP I'm trying to run a SQL query and select normal columns as well as COUNT.
$sql_str = "select COUNT(DISTINCT name), id, adress from users";
$src = mysql_query($sql_str);
while( $dsatz = mysql_fetch_assoc($src) ){
echo $dsatz['name'] . "<br>";
}
The problem is that when I have "COUNT(DISTINCT name)," in my query, it will only return the first entry. When I remove it, it will return all matching entries from the db.
I could separate it and do 2 queries, but I'm trying to avoid this due to performance concerns.
What do I make wrong?
thx, Mexx
The ability to mix normal columns and aggregate functions is a (mis)feature of MySQL.
You can even read why it's so dangerous on MySQL's documentation:
https://dev.mysql.com/doc/refman/5.6/en/group-by-extensions.html
But if you really want to mix normal rows and a summary in a single query, you can always use the UNION statement:
SELECT COUNT(DISTINCT name), null, null FROM users GROUP BY name --summary row
UNION
SELECT name, id, address FROM users --normal rows
COUNT() is an aggregate function, it aggregates against the results of the rest of your query. If you want to count all distinct names, and not just the distinct names associated with the id and address that you are selecting, then yes, you will have to run two queries. That's just how SQL works.
Note that you should also have a group by clause when aggregating. I think the fact that MySQL doesn't require it is horrible, and it encourages really bad habits.
From what I understand, you want to get :
one line per user, to get each name/id/address
one line for several users at the same time, to get the number of users who have the same name.
This is not quite possible, I'd say.
A solution would be, like you said, two queries...
... Or, in your case, you could do the count on the PHP side, I suppose.
ie, not count in the query, but use an additionnal loop in your PHP code.
When you have a count() as part of the field list you should group the rest of the fields. In your case that would be
select count(distinct name), id, adress from users group by id, adress
select count(distinct name), id, adress
from users
group by id, adress
I'm assuming you want to get all your users and the total count in the same query.
Try
select name, id, address, count(id) as total_number
from users
group by name, id, address;