SQL query column with cookie data - php

My cookie member names are the first and last name of the user with a space in between. I'm trying to get the user ID from the row with the following query:
SELECT * FROM dbo.users WHERE firstname IN('{$_COOKIE['member_name']}') AND lastname IN('{$_COOKIE['member_name']}')
I'm not getting any results from this, and I can't figure out what I'm doing wrong. The LIKE operator isn't working either.

why using IN() ?
i have try like this query, can work for my data
maybe you can check table column data type and output query SQL.
(root#localhost) [test]> select * from a;
+------+------+-------+
| team | type | value |
+------+------+-------+
| A | 0 | 10 |
| A | 1 | 5 |
| B | 0 | 10 |
| B | 0 | 10 |
| A | 1 | 20 |
| B | 1 | 20 |
+------+------+-------+
6 rows in set (0.00 sec)
(root#localhost) [test]> select * from a where team in ('A') and type in ('0');
+------+------+-------+
| team | type | value |
+------+------+-------+
| A | 0 | 10 |
+------+------+-------+
1 row in set (0.00 sec)

Related

Select from two or more sql tables where have some column names

I have 6 table where all columns name is same. Now I want to select only those table where new = '1'.
For example:
Table 1: 3d_movie
+----+------+-----+------+
| id | name | new | date |
+----+------+-----+------+
| 1 |name1 | 0 | 10.00|
| 2 |name1 | 0 | 10.00|
| 3 |name2 | 1 | 10.00|
+----+------+-----+------+
Table 2: english_movie
+----+------+-----+------+
| id | name | new | date |
+----+------+-----+------+
| 1 |name1 | 0 | 10.00|
| 2 |name1 | 1 | 10.00|
| 3 |name2 | 0 | 10.00|
+----+------+-----+------+
Table 3: hindi_movie
+----+------+-----+------+
| id | name | new | date |
+----+------+-----+------+
| 1 |name1 | 1 | 10.00|
| 2 |name1 | 0 | 10.00|
| 3 |name2 | 0 | 10.00|
+----+------+-----+------+
When admin add a new movie on his server, he select new = 1.
So if client go to new movies page, I want to search all movie from all table where new = '1'.
I tried by these way but it's not work, please help me..
Tried 1: $result = mysqli_query($db,"SELECT * FROM 3d_movie,english_movie,hindi_movie WHERE 3d_movie.new='1' and english_movie.new='1' and hindi_movie.new='1 ORDER BY date DESC LIMIT 30");
Tried 2: $result = mysqli_query($db,"SELECT * FROM 3d_movie,english_movie,hindi_movie WHERE new='1' ORDER BY date DESC LIMIT 30");
Select using UNION
SELECT *
FROM 3d_movie
WHERE new = 1
UNION
SELECT *
FROM 3d_movie
WHERE new = 1
and so on
That is the easiet way, but in the future you should consider combining the tables and adding one column do identify the movie genre:
Table 1: movie
+----+------+-----+------+-------+
| id | name | new | date | genre |
+----+------+-----+------+-------+
| 1 |name1 | 0 | 10.00| 3d |
| 2 |name1 | 0 | 10.00| eng |
| 3 |name2 | 1 | 10.00| hin |
+----+------+-----+------+-------+
You have to frame a SQL query using UNION. Try the following query:
SELECT *
FROM 3D_MOVIE
WHERE NEW = 1
UNION
SELECT *
FROM ENGLISH_MOVIE
WHERE NEW = 1
UNION
SELECT *
FROM HINDI_MOVIE
WHERE NEW = 1
You can run SQL query among your tables to get your desired output. However, better solution to your problem would be if you can change your table structure. Rather than keeping multiple tables you can simply add another column like moview_type or category and put all movie list in the same table. So your table will look like the below:
+----+------+-----+------+----------+
| id | name | new | date | category |
+----+------+-----+------+----------+
| 1 |name1 | 0 | 10.00| 3d |
| 2 |name1 | 0 | 10.00| English |
| 3 |name2 | 1 | 10.00| Hindi |
+----+------+-----+------+----------+
This way you do not have to add new tables if you have a new types of movie.

Query mysql when reach row then stop? show the queue number

I am doing a script want to calculate how many row record before an user record when t1.status is 1.
My table is t1, and the data as below:
+------+---------+------------+----------+----------+
| ID | name | desc | status | time |
+------+---------+------------+----------+----------+
| 1 | ABB | | 1 | 0325 |
| 2 | CCD | | 1 | 0236 |
| 3 | EEF | | 1 | 0325 |
| 4 | GGG | | 1 | 0000 |
| 5 | HIJ | | 2 | 1234 |
| 6 | KKK | | 1 | 5151 |
+---------------------------------------------------+
I was thinking about the query is something like (query row where status = 1 AND stop when reach $userid)
I would like to output to show user (Let's say username is GGG) as:
$userid = 'GGG';
then my output will be
<table><tr><td>Queue: GGG You came in 4 place, in front of you still got 3 person in queue, please be patient</td></tr></table>
How to I do the right query to get the number 4 and 3 ?
Thank you.
You can try something like this hope it helps :-
SELECT count(*) as COUNT FROM t1 WHERE id < (SELECT id FROM t1 WHERE userid = $userid)

Retrieve last 3 years of data with custom date

Here's my query:
mysql> select * from jobs where datediff(now(),str_to_date(last_modified,'%M %d,%Y'))>=1095;
I get 0 results
mysql> select max(last_modified) from jobs;
+--------------------+
| max(last_modified) |
+--------------------+
| 9/9/2013 |
+--------------------+
1 row in set (0.06 sec)
mysql>
It doesn't seem to be working to well and Im not sure why. I think it has to do with the original formatting of the last_modified column
UPDATE
mysql> select distinct(last_modified) from jobs where datediff(now(),str_to_date(last_modified,'%m/%d/%Y'))>=1095 limit 10;
+---------------+
| last_modified |
+---------------+
| 12/4/2003 |
| 12/5/2003 |
| 12/6/2003 |
| 12/8/2003 |
| 12/9/2003 |
| 12/10/2003 |
| 12/11/2003 |
| 12/12/2003 |
| 12/13/2003 |
| 12/14/2003 |
+---------------+
10 rows in set (0.00 sec)
mysql>
Use str_to_date(last_modified,'%m/%d/%Y') instead of str_to_date(last_modified,'%M %d,%Y')

Cron job to identify multiple visits in the same url?

using a PHP cron job, how can I output the users who visited the same page over 2 times ?
Here is the mySQL table
id - user - page - timestamp
340 - 1 - page1 - 2009-05-18 22:11:11
339 - 1 - page1 - 2009-05-18 22:10:01
337 - 1 - page1 - 2009-05-18 22:06:00
336 - 2 - page3 - 2009-05-18 22:00:00
335 - 2 - page3 - 2009-05-18 21:56:00
This could be done easily with group by and count
select
user,page, count(*) as total
from table_name
group by user,page
having total > 2
UPDATE
how can i update another table with these results? user - page - times
This could be done using insert into select from in addition using on duplicate key update
Consider the following
mysql> select * from pages ;
+------+------+
| user | page |
+------+------+
| 1 | p1 |
| 1 | p1 |
| 1 | p1 |
| 2 | p1 |
| 2 | p1 |
| 2 | p2 |
| 3 | p2 |
| 3 | p2 |
| 3 | p2 |
+------+------+
create table page_view_log
(
user int,
page varchar(100),
times int,
primary key(user,page)
);
select * from page_view_log ;
Note that here the log table has composite key (user,page)
Now the following query will do the job
insert into page_view_log(user,page,times)
select
user,page,total from (
select user,page, count(*) as total
from pages
group by user,page
having total > 2
)x
on duplicate key update times = total
mysql> select * from page_view_log ;
+------+------+-------+
| user | page | times |
+------+------+-------+
| 1 | p1 | 3 |
| 3 | p2 | 3 |
+------+------+-------+
2 rows in set (0.00 sec)
Now lets add more page in the first table for user-1
insert into pages values (1,'p1'),(2,'p2'),(2,'p2');
Now if we run the above query again it will update the total for existing data and insert new values, the duplicate is considered using the composite key (user,page)
mysql> select * from page_view_log ;
+------+------+-------+
| user | page | times |
+------+------+-------+
| 1 | p1 | 4 |
| 2 | p2 | 3 |
| 3 | p2 | 3 |
+------+------+-------+

MYSQL RANDOM SELECT UNIQUE ROWS - Excluding previously selected rows

I have a table of 16K entries
I want to extract random 44 entries
but I don't want to repeat the same entries more then once (ever)
so i have a per-user list that keeps the already used 'IDs' as a comma-separated string in a table.
and I use that list to SELECT ... NOT IN (used_IDs)
The issue is that this list is getting too big and the sql call fails because of size i believe
Any idea on how to do that more usefully?
Questions table:
+------+-------+-------+
| id | Qtext | Tags |
+------+-------+-------+
Test table:
+------+-------+
| id | QIDs |
+------+-------+
Results table:
+------+-------+-------+
| id | tID | uID |
+------+-------+-------+
I need to select unique random values from Questions table based on the results table. (which associates test ID with Question IDs)
Currently trying to use:
SELECT DISTINCT `questions`.`ID`
FROM `questions`, `tests`, `results`
WHERE
`questions`.`ID` NOT IN (`tests`.`qIDs`)
AND `results`.`uID` = 1 AND `tests`.`ID` = `results`.`tID`
AND 4 IN ( `questions`.`tags`)
AND "http://www.usmlestep2qna.com" = `provider`
ORDER BY RAND() LIMIT 27;
Any ideas?
Instead of placing the used user Id values in a comma-separated string in one column, you could create a tall table to store them. This should yield better preformance
Rather than using a single row with a (potentially huge) CSV, why not use a nicely indexed table and an outer join to pick unmatched records. I have an example from my test database:
mysql> select * from first;
+------+-------+
| id | title |
+------+-------+
| 1 | aaaa |
| 2 | bbbb |
| 3 | cccc |
| 4 | NULL |
| 6 | gggg |
+------+-------+
5 rows in set (0.00 sec)
mysql> select * from second;
+------+----------+------+------+-------+------+
| id | first_id | one | two | three | four |
+------+----------+------+------+-------+------+
| 1 | 1 | 3 | 0 | 4 | 6 |
| 1 | 2 | 4 | 4 | 1 | 2 |
| 3 | 3 | 1 | NULL | 3 | 4 |
+------+----------+------+------+-------+------+
3 rows in set (0.00 sec)
mysql> select a.id from first a join second b on a.id=b.first_id;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
mysql> select a.id from first a
left outer join second b on a.id=b.first_id where b.first_id is null;
+------+
| id |
+------+
| 4 |
| 6 |
+------+
2 rows in set (0.00 sec)
This should improve your performance rather nicely.

Categories