I tried making a SQL query and union the result on the current time, but I cannot seem to find a neat way to solve this.
I've tried the following:
SELECT * FROM `accounts`
UNION SELECT NOW()
And Sequel Pro just reports The used SELECT statements have a different number of columns.
The accountstable just has three columns:
ID (INT(32), AUTO_INC)
CREATED (Timestamp)
NAME (VAR_CHAR(28))
I anticipated I'd get a response with four columns: ID, CREATED, NAME, NOW
What do I do wrong?
Union means that the records from the second query will be appended to those retrieved from the first one.
So the two tables must have the same structure for this to work.
For example:
SELECT field1,field2,field3 FROM tableA
UNION
SELECT field1,field2,field3 FROM tableB
What you want to do is
SELECT *, NOW() as now FROM `accounts`
This will retrieve all the records from the accounts table and will add the timestamp to all the rows on a column named "now" (this is just an alias so use whatever you like).
try this
SELECT *,now() as now FROM `accounts`
Related
I need some assistance in merging these 2 sql queries into one and get the same result.
Database structure:
Table: sqldb1_meta
Columns: ipaddress, data
First query:
SELECT * FROM sqldb1_meta WHERE ipaddress LIKE '%10.10.1.2%'
From the first query i get all that matches coulmn ipaddress 10.10.1.2, the other column has data for this entry, in this case it was '92"
Second Query:
SELECT * FROM sqldb1_meta WHERE data LIKE '92'
Now i get all the data i want displayed, everything that had value 92 from column data. I can only know it was 92 based on the first query.
How do i combine this into one query instead of having to run 2?
Need more details, but maybe inner join the table to it self (self join) or union...
Use your first Select query as a Subquery in the Where condition.
SELECT `ipaddress`,
`data`
FROM sqldb1_meta
WHERE `data` IN (
SELECT `data`
FROM sqldb1_meta
WHERE `ipaddress` LIKE '%10.10.1.2%'
)
Based on your post, you just need to combine both conditions on the where clause using the and operator
SELECT * FROM sqldb1_meta WHERE ipaddress LIKE '%10.10.1.2%' and data LIKE '92'
I'm trying to create an extra column on my SQL where I could identify if the user_id generated a subsequent (or even third) row.
And if so, calculate the datediff between the connection time and previous row disconnection time.
If no duplicate user_id, the response should be NULL.
Here's a screenshot of my data and notes:
I tried the DATEDIFF formula but no success.
Could someone help me on this? I really would appreciate any input.
SELECT id,
user_id,
connected_at,
disconnected_at,
IFNULL(DateDiff('second', Lookup(disconnected_at), -1), connected_at)
FROM data
ORDER BY id, user_id, connected_at
When comparing values in different rows, you need to join the table to itself to get the second row. Try this:
SELECT
a.id,
a.user_id,
a.connected_at,
a.disconnected_at,
DateDiff('second',b.`connected_at`,a.`disconnected_at`) as `time_diff`
FROM `data` a
JOIN `data` b
ON a.`user_id` = b.`user_id` AND b.`connected_at` > a.`disconnected_at`
In my table I have several duplicates. Ineed to find unique values in mysql table column.
SQL
SELECT column FROM table
WHERE column is unique
SELECT column FROM table
WHERE column = DISTINCT
I've been trying to Google, but almost all queries are more complex.
The result I's like is all non duplicate values.
EDIT
I'd like to have UNIQUE values...
Not all values one time... (Distinct)
Try to use DISTINCT like this:
SELECT DISTINCT mycolumn FROM mytable
EDIT:
Try
select mycolumn, count(mycolumn) c from mytable
group by mycolumn having c = 1
Here is the query that you want!
SELECT column FROM table GROUP BY column HAVING COUNT(column) = 1
This query took 00.34 seconds on a data set of 1 Million rows.
Here is a query for you though, in the future if you DO want duplicates, but NOT non-duplicates...
SELECT column, COUNT(column) FROM table GROUP BY column HAVING COUNT(column) > 1
This query took 00.59 seconds on a data set of 1 Million rows. This query will give you the (column) value for every duplicate, and also the COUNT(column) result for how many duplicates. You can obviously choose not to select COUNT(column) if you don't care how many there are.
You can also check this out, if you need access to more than just the column with possible duplicates... Finding duplicate values in a SQL table
Try this one:
SELECT COUNT(column_name) AS `counter`, column_name
FROM tablename
GROUP BY column_name
WHERE COUNT(column_name) = 1
Have a look at this fiddle: http://sqlfiddle.com/#!9/15147/2/0
Try this:
SELECT DISTINCT (column_name) FROM table_name
I have two tables.
I query like this:
SELECT * FROM (
Select requester_name,receiver_name from poem_authors_follow_requests as one
UNION
Select requester_name,receiver_name from poem_authors_friend_requests as two
) as u
where (LOWER(requester_name)=LOWER('user1') or LOWER(receiver_name)=LOWER('user1'))
I am using UNION because i want to get distinct values for each user if a user exists in the first table and in the second.
For example:
table1
nameofuser
peter
table2
nameofuser
peter
if peter is on either table i should get the name one time because it exists on both tables.
Still i get one row from first table and a second from table number two. What is wrong?
Any help appreciated.
There are two problems with your SQL:
(THis is not the question, but should be considered) by using WHERE over the UNION instead of the tables, you create a performance nightmare: MySQL will create a temporary table containing the UNION, then query it over the WHERE. Using a calculation on a field (LOWER(requester_name)) makes this even worse.
The reason you get two rows is, that UNION DISTINCT will only suppress real duplicates, so the tuple (someuser,peter) and the tuple (someotheruser, peter) will result in duplication.
Edit
To make (someuser, peter) a duplicate of (peter, someuser) you could use:
SELECT
IF(requester_name='peter', receiver_name, requester_name) AS otheruser
FROM
...
UNION
SELECT
IF(requester_name='peter', receiver_name, requester_name) AS otheruser
FROM
...
So you only select someuser which you already know : peter
You need the where clause on both selects:
select requester_name, receiver_name
from poem_authors_follow_requests
where LOWER(requester_name) = LOWER('user1') or LOWER(receiver_name) = LOWER('user1')
union
select requester_name, receiver_name
from poem_authors_friend_requests
where LOWER(requester_name) = LOWER('user1') or LOWER(receiver_name) = LOWER('user1')
The two queries are independent of each other, so you shouldn't try to connect them other than by union.
You can use UNION if you want to select rows one after the other from several tables or several sets of rows from a single table all as a single result set.
UNION is available as of MySQL 4.0. This section illustrates how to use it.
Suppose you have two tables that list prospective and actual customers, a third that lists vendors from whom you purchase supplies, and you want to create a single mailing list by merging names and addresses from all three tables. UNION provides a way to do this. Assume the three tables have the following contents:
http://w3webtutorial.blogspot.com/2013/11/union-in-mysql.html
You are doing the union before and then applying the where clause. So you would get a unique combination of "requester_name,receiver_name" and then the where clause would apply. Apply the where clause in each select...
Select requester_name,receiver_name from poem_authors_follow_requests
where (LOWER(requester_name)=LOWER('user1')
or LOWER(receiver_name)=LOWER('user1'))
UNION
Select requester_name,receiver_name from poem_authors_friend_requests
where (LOWER(requester_name)=LOWER('user1')
or LOWER(receiver_name)=LOWER('user1'))
In your where statement, reference the alias "u" for each field refence in your where statement.
So the beginning of your where statement would be like: where (LOWER(u.requester_name) = ...
This is simlar to the answer you can see in: WHERE statement after a UNION in SQL?
You should be able to use the INTERSECT keyword instead of doing a nested query on a UNION.
SELECT member_id, name FROM a
INTERSECT
SELECT member_id, name FROM b
can simply be rewritten to
SELECT a.member_id, a.name
FROM a INNER JOIN b
USING (member_id, name)
http://www.bitbybit.dk/carsten/blog/?p=71
I have two tables with identical structures handling distinct data. I want to merge them, add a text field indicating where the data for that row came from, and order by a common field.
TABLE1
ID|NAME|YEAR
1,'peter',2008
2,'edward',2010
TABLE2
ID|NAME|YEAR
1,'compadre',2009
2,'vika',2011
DRAFT of query ( obviously is erroneous )
select * from TABLE1 JOIN TABLE2 order by YEAR asc
expected result:
1,'peter','iamfromTABLE1',2008
1,'compadre','iamfromTABLE2',2009
2,'edward','iamfromTABLE1',2010
2,'vika','iamfromTABLE2',2011
I know I can do this using PHP/MySQL, but is there not a more elegant way like the "One Simple Query".
Use a Union query and literals:
SELECT ID, Name, 'iamfromTABLE1' as indicator, Year
FROM Table1
UNION
SELECT ID, Name, 'iamfromTABLE2' as indicator, Year
FROM Table2
ORDER BY Year
EDIT: as indicator added on recommendation of iim.hlk