0

I have a funky query that works fine with static data but I need my data to be dynamic. So the static data is like this

SELECT c.my_name, c.my_id, (SELECT count(d.friendship_id) FROM another_table d WHERE d.my_id = 1 AND d.my_friends_id = 2) as count FROM myprofile c WHERE c.my_id = 1;

This returns the data I want like this:

my_name   my_id   count
parijat    123     1 (OR 0 if the row doesn't exist)

For reference, both another_table.my_id (foreign key), another_table.my_friends_id references myprofile.my_id (primary key). another_table.friendship_id is the primary key here and is auto incremented.

Now the actual question:

I want my subquery to be something like this: (SELECT count(d.friendship_id) FROM another_table d WHERE d.my_id = 1 AND d.my_friends_id = CURRENT_ROW_ID)

where CURRENT_ROW_MY_ID is the c.my_id that is being selected upon in the main query.

Is this possible and if not, what should my approach be to get the results I need ?

4

2 回答 2

0

您可以执行子查询来获取该表的当前 auto_increment 值:

select auto_increment from information_schema.tables where table_schema = 'you_db_name' and table_name = 'your_table_name'

高温高压

弗朗西斯科

于 2013-04-04T21:46:27.940 回答
0

有时我会在完全探索该选项之前询问。刚刚发现相关子查询即使在选择语句中也能正常工作。这是我为使其正常工作所做的工作:

SELECT c.my_name, c.my_id, (SELECT count(d.friendship_id) FROM another_table d WHERE d.my_id = 1 AND d.my_friends_id = c.my_id) as count FROM myprofile c WHERE c.my_id = 1;

my_id 有点模棱两可。一个更好的词是 profile_id,但是处理遗留数据库肯定不是很有趣。

于 2013-04-04T22:06:28.073 回答