Simple query, i have a table with clientID and cardKey. One client can have many cards. Query is to find all cards belonging to client 1
I am using mysql work bench, it executes the query normally with no errors but returns no results
SELECT cID, cardKey
FROM client_cards
where `cID` = 1 ;
Your query is correct, but in the client_cards table there is no row that has cID = 1.
Related
Notes:
The return from SELECT version() is 10.5.12-MariaDB-log
Default collation: utf8mb4_unicode-ci
Default charset: utf8mb4
Queries run using MySQL Workbench for Ubuntu Linux 8.0.29
My goal is to delete duplicated items in a table. I do not have any other table to use as a reference to check duplicates. I created a simple query and subquery that returns expected results:
SELECT * FROM messages WHERE id NOT IN
(SELECT id FROM
messages
WHERE
uid = '11899414026778263'
GROUP BY message_id , uid
ORDER BY created_at);
Despite setting SQL_SAFE_UPDATES to 0, a DELETE operation using the same data fails. I get a "QUERY Interrupted" message.
SET SQL_SAFE_UPDATES = 0;
DELETE FROM messages WHERE id NOT IN
(SELECT id FROM
messages
WHERE
uid = '11899414026778263'
GROUP BY message_id , uid
ORDER BY created_at);
SET SQL_SAFE_UPDATES = 1;
If I replace DELETE with SELECT *, the query returns results. Another StackOverflow answer said that querying based on a sub-query does not work in MySQL. Others say to use another table as reference instead of a subquery.
DELETE query results in 'Query Interrupted' MySQL Workbench?
This method works in some SQL implementations based on these answers and websites:
Delete Duplicates From a Table in SQL Server
Different ways to SQL delete duplicate rows from a SQL Table
How to delete duplicate rows in SQL Server?
order by is applied after grouping, so your order by is not sufficient to select the id with the lowest created_at for each group. Your query will fail under ONLY_FULL_GROUP_BY because the id returned by the subselect will be arbitrary within each group. You want to use first_value instead.
But it's easier just to not use a subquery:
delete m
from messages m
# is there a message we would prefer over this one?
inner join messages m2 on (m2.uid,m2.message_id)=(m.uid,m.message_id) and (m2.created_at,m2.id) < (m.created_at,m.id)
I have two different MySQL count queries like this:
SELECT COUNT(*) AS stored_counter FROM users WHERE ***
SELECT COUNT(*) AS total_taken FROM completed WHERE ***
I'm looking for if the count value of query 1 matches the count value of query 2. Right now I am running these two queries seperately via PHP and then using PHP to see if the two queries return the same values.
But i'm looking to see if it's possible to do this with 1 SQL query?
Thanks
You can use this form
SELECT
(SELECT COUNT(*) FROM users WHERE 1 =1) stored_counter,
(SELECT COUNT(*) FROM completed WHERE 1=1) total_taken
You can add every Select statement that only gives back one Result (one field(one row one column)) only) as its own column
initially i have a empty table in a database named comments.
no row input given here.
it has 4 columns, 3 foreign key of those.
i wrote a query on it like that -
SELECT COUNT(*)
FROM `comments`
GROUP BY posts_id
it returns nothing because the table is completely empty.
but i must need at least 0 by this query for confirming table is empty or if it has value then return me the value.
or any other option to check that the table is null , it has no row.
(currently i am testing the query in mysql. later, i will convert it in Laravel query builders and use it in a laravel app)
Use UNION ALL with a query that returns 0 in case the table is empty:
SELECT COUNT(*)
FROM `comments`
GROUP BY posts_id
UNION ALL
SELECT 0 FROM dual
WHERE NOT EXISTS (SELECT 1 FROM `comments`)
If you are using MySql 8.0+ you can omit FROM dual.
I am using Doctrine with Zend Framework 2 to do a query on my invoices table using a sub-query.
Here's the simplified generated SQL with parameters filled in
SELECT *
FROM invoices i1
WHERE (EXISTS (SELECT * FROM invoices i2 WHERE i2.invoice_first_name IN ('stephen') OR i2.invoice_middle_name IN ('stephen') OR i2.invoice_surname IN('stephen')))
ORDER BY i1.invoice_id DESC
The problem I am having is that when I run this query in phpMyAdmin it returns all the invoices, even those that do not contain the name 'stephen'.
However when I run the sub-query separately it returns 2 rows which is correct.
SELECT * FROM invoices i2 WHERE i2.invoice_first_name IN ('stephen') OR i2.invoice_middle_name IN ('stephen') OR i2.invoice_surname IN('stephen')
So why doesn't the sub-query work with EXISTS, what am I missing?
Many thanks in advance.
As per MySQL documentation:
If a subquery returns any rows at all, EXISTS subquery is TRUE, and NOT EXISTS subquery is FALSE
Since your subquery returns some row(s), the where clause is true - for every row in the invoice table.
I have a table that contains event information for users, i.e. each row contains an ID, a user's start date, completion date, and the event number. Example:
Row 1: ID 256 | StartDate 13360500 | FinishDate 13390500 | EventNum 3
I am trying to intersect all of the rows for users who have finished events 3 & 2, but I can't figure out why my query is returning no results:
SELECT table_id FROM table
WHERE table_EventNum = 3
AND table_FinishDate > 0
AND table_id IN (SELECT table_id FROM table WHERE table_EventNum = 2);
This query without the subquery (the line separated from the rest at the bottom) returns a bunch of non-null results, as it should, and the subquery also returns a bunch of non-null results (again, as it should). But for some reason the composite query doesn't return any rows at all. I know the IN command returns NULL if the expression on either side is NULL, but since both queries return results I'm not sure what else might cause this.
Any ideas? Thanks!
Assuming FinishDate is NULL when the event is not complete. Also assuming that there has to be a row with matching id and event number 2 and that event 3 cannot happen before event 2:
SELECT t1.table_id FROM table t1 INNER JOIN table t2 ON t1.table_id = t2.table_id
WHERE t1.table_EventNum = 3 AND t2.table_EventNum = 2
AND NOT t1.table_FinishDate IS NULL
Note that I could not find anything wrong with your query other than the fact that you do not need a subquery.