How can I get just one result from multiple in MySQL?
SELECT DATE_ADD(data, INTERVAL(-WEEKDAY(data)) DAY) AS D
FROM Done
GROUP BY D
It gives me "2017-08-07" and "2017-08-14" and I need them both, but one at the start and the other at the end. How can I do this?
The result from the query is called result set in terms of database.
So, your result set contains 2 rows, or in other words you can say that there are 2 results in the result set from this query.
In fact you have them separate - each on its own row in the result set.
When fetching the result in the programming language you are using, you will receive them as a collection or array.
Then, you can get the first and the second element of that collection/array and print them as desired.
Try This :-
DROP TABLE IF EXISTS tbl_1;
CREATE TEMPORARY TABLE tbl_1
AS
SELECT
DATE_ADD(data, INTERVAL(-WEEKDAY(data)) DAY) AS D,#row_num:=#row_num+1 as row_id
FROM
Done,(select #row_num:=0)rownum
GROUP BY D;
DROP TABLE IF EXISTS tbl_2;
CREATE TEMPORARY TABLE tbl_2
AS
SELECT * from tbl_1;
SELECT tbl_2.D,tbl_1.D from tbl_1
JOIN tbl_2 ON tbl_1.row_id=tbl_2.row_id+1
Related
I am having two table
Table 1 having a field
id
book_ids
1
1,2,3
Table 2 have all the book Ids
select *
from table 2
where book_id in (select book_ids from table 1 where id=1) ;
this statement not returning all the book ids from table 2 having id 1,2,3
Can anyone help
You could use the FIND_IN_SET() function:
select *
from table 2
where FIND_IN_SET(book_id, (select book_ids from table 1 where id=1)) > 0;
Read the documentation I linked to for details on how that function works.
But only do this if your table remains small. Using this function spoils any opportunity to optimize the query with an index, so the larger your table gets, the performance will grow worse and worse.
Also FIND_IN_SET() doesn't work the way you expect if there are spaces in your comma-separated list.
try to store table1 values in rows of table not in a same field.
and then your SELECT IN works.
I have a simple MYSQL table with about 5 columns or so. The row size of the table changes quite frequently.
One of these columns is named has_error, and is a column that has a value of either 1 or 0.
I want to create a single SQL query that will be the equivalent of the following simple equation:
(Number of rows with has_error = 1/Total number of rows in table) * 100
I can create the individual SQL queries (see below), but not sure how to put it all together.
SELECT COUNT(*) AS total_number_of_rows FROM my_table
SELECT COUNT(*) AS number_of_rows_with_errors FROM My_table WHERE has_error = 1
This is easy because you can just use avg(has_error):
SELECT AVG(has_error) * 100
FROM My_table;
select *
from AllUK
where exists (select * from AllCompanies where replace(AllUK.mobile,' ','')=replace(AllCompanies.mobile,' ',''))
I need to include the columns from the AllCompanies table in to my first select. How can I do that?
select *
from AllUK a
join AllCompanies b
on a.mobile = b.mobile
exists is a boolean operation, so the clause you have above will always return all the results if there any records that can be joined accross the 2 tables. It's hard to tell what you're really trying to achieve.
Also, putting string operations on columns within exists and joins is not best practice because the compiler has to do the operation on every row & column at run time. Might be better to create a temp table to hold the replaced values and then join on that.
I need help with CONCAT function. I have two select queries and the result of every query is one column. I need to merge this two columns in one. Is that possible? Beacuse, I can't get result even if I try with simple select queries like:
SELECT owner FROM table WHERE number="value1";
SELECT number FROM table WHERE owner="value2" AND number IS NOT null;
These queries work and throw 3 rows like result. But, if I want to merge them in one column using CONCAT - that doesn't work. Do you know why?
SELECT CONCAT(SELECT owner FROM table WHERE number="value1",
SELECT number FROM table WHERE owner="value2" AND number IS NOT null
) as NEW_COLUMN FROM table;
I think you want this:
SELECT CONCAT(owner, number) newCol1
FROM yourTable
WHERE number="value1"
OR (owner="value2" AND number IS NOT null)
SELECT
CONCAT(owner, number) as NEW_COLUMN
FROM
table
WHERE
owner = "value2"
AND number = "value1"
AND number IS NOT NULL
The fundamental reason is that the DB cannot concatenate two different SELECTs which might have a different number of rows.
What you need to do is to re-formulate your query in terms of a JOIN.
For example suppose we have this table:
owner number
John value1
value2 123456
Your first query:
SELECT owner FROM table WHERE number="value1";
will return "John". The second one
SELECT number FROM table WHERE owner="value2" AND number IS NOT null;
will return "123456".
If you CONCAT the two values you would therefore get "John 123456".
First of all, is this the expected behaviour of the query you want? What happens is there is a third row with owner=Jack and number=value1, so that the first query returns TWO rows "John" and "Jack"?
One thing you could look into is the CROSS JOIN syntax.
SELECT CONCAT (table1.owner, ', ', table2.number) AS new_column
FROM ( SELECT owner FROM table WHERE number="value1" ) AS tablel1
CROSS JOIN
(SELECT number FROM table WHERE owner="value2" AND number IS NOT null ) AS table2;
Note that if the first query returns three rows and the second four rows, the combined query will return 3*4 = 12 rows.
I am not sure if this is possible but here goes.
I am using MySQL and need to complete a statement that produces an output as follows.
I have a table which contains a completeddate field and an enquirydate field.
I need to get data on the time difference between theses fields for which I have the following code
SELECT DATE (completedate) - DATE (enqdate) AS `timediff`, COUNT(*) AS TotalCount
FROM cia_enquiry
WHERE (DATEDIFF(CURDATE(), enqdate) < 30) AND completedate > 1
GROUP BY `timediff`
ORDER BY `timediff` ASC
The above code simply outputs a figure of days between the dates and the total count of entires within the last 30 days (and not counting those entries that are not complete)
I now need to reference the "timediff" results against another table and pull another field from that table.
For example if timediff = 1, then we need to find the id of 1 in another table and return the description field from this table.
Ultimately we should end up with something similar to
timediff TotalCount description
0 52 Within 24 hrs
1 13 24-48hrs
etc etc .........
It can be done; you have two choices
Create a temporary table with the result of your query and then join this table with your second table
Use your first query as a subquery and join it with your second table
Option 1
create temporary table temp_tbl
select
...
;
# Important: create the indexes you need
alter table temp_tbl
add index index1(field1), ... ;
# Now join your second table
select a.*, b.*
from temp_tbl as a join tbl2 as b on ...;
Option 2
select a.*, b.*
from (
select ... # here is your query
) as a join tbl2 as b on ...
Hope this helps you
The date calculation will give you a bunch of decimal points worth of values...
you should use ROUND()
or look up CEIL and FLOOR functions.